博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS 关于上传图片裁剪以及压缩,确保高清
阅读量:7056 次
发布时间:2019-06-28

本文共 1425 字,大约阅读时间需要 4 分钟。

// 绘制图片

-(NSData *) imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = size.width;
    CGFloat targetHeight = size.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
    if(CGSizeEqualToSize(imageSize, size) == NO){
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        if(widthFactor > heightFactor){
            scaleFactor = widthFactor;
        }
        else{
            scaleFactor = heightFactor;
        }
        scaledWidth = width * scaleFactor;
        scaledHeight = height * scaleFactor;
        if(widthFactor > heightFactor){
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }else if(widthFactor < heightFactor){
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    
    UIGraphicsBeginImageContext(size);
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    [sourceImage drawInRect:thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    if(newImage == nil){
        NSLog(@"scale image fail");
    }
    
    UIGraphicsEndImageContext();
    NSLog(@"==---==%@",newImage);
    return UIImageJPEGRepresentation(newImage, 1);
    
}

转载于:https://www.cnblogs.com/yangxiaolong/p/5435012.html

你可能感兴趣的文章
Shell练习(十二)
查看>>
解决springmvc+mybatis+mysql中文乱码问题【转】
查看>>
十三,十四单元总结
查看>>
表单布局
查看>>
Centos 6.6 下 nginx +php mysql + phpMyadmin 安装部署
查看>>
device-mapper 块级重删(dm dedup) <3>代码结构(2)
查看>>
cas_client之AuthenticationFilter源码分析
查看>>
脉冲云之持续集成(上篇)
查看>>
Linux LVM硬盘管理及LVM扩容
查看>>
如何进行大数据入门的学习
查看>>
四个核心技术构成智能语音系统
查看>>
Oracle技术之Toad使用技巧
查看>>
C# 如何更改Word语言设置
查看>>
oracle database 10.2.0.4 升级到 10.2.0.5
查看>>
Block
查看>>
在隔离的局域网内部架设基于CentOS7的NTP服务器
查看>>
通过房地产、医院、眼镜店、甚至饭店、理发店来对比,请你尊重你的劳动成果做软件不要开出跳楼价...
查看>>
运维学python之爬虫中级篇(一)进程
查看>>
Linux下多线程,断点续传,命令行下载工具axel
查看>>
putty和ssh远程登陆报错的解决方法
查看>>