博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS_画图 图片等比压缩 IOS_UIImage
阅读量:4564 次
发布时间:2019-06-08

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

- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{        // 创建一个bitmap的context        // 并把它设置成为当前正在使用的context        UIGraphicsBeginImageContext(size);        // 绘制改变大小的图片        [img drawInRect:CGRectMake(0,0, size.width, size.height)];        // 从当前context中创建一个改变大小后的图片        UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();        // 使当前的context出堆栈        UIGraphicsEndImageContext();        //返回新的改变大小后的图片        return scaledImage;    }

压缩成指定大小代码

 

 

 

等比压缩

等比例压缩-(UIImage *) 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();        return newImage;    }-(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{    UIImage *newImage = nil;    CGSize imageSize = sourceImage.size;    CGFloat width = imageSize.width;    CGFloat height = imageSize.height;    CGFloat targetWidth = defineWidth;    CGFloat targetHeight = height / (width / targetWidth);    CGSize size = CGSizeMake(targetWidth, targetHeight);    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();    return newImage;}

 

转载于:https://www.cnblogs.com/yswdarren/p/3611934.html

你可能感兴趣的文章
Sublimetext3安装Emmet插件步骤
查看>>
MySQL配置参数
查看>>
全面理解Java内存模型
查看>>
A - Mike and palindrome
查看>>
DOTween教程
查看>>
java web中java和python混合使用
查看>>
创建学员类和教员类
查看>>
Cookie和Session的作用和工作原理
查看>>
字符串操作
查看>>
Visual Studio中改变environment 的布局和显示风格
查看>>
2016-XCTF Final-Richman
查看>>
文件下载
查看>>
extjs grid renderer用法
查看>>
vue 如何在循环中绑定v-model
查看>>
shell脚本
查看>>
[代码笔记]JS保持函数单一职责,灵活组合
查看>>
cmd 重定向
查看>>
【IOS开发】如何画1像素的线
查看>>
【计算机视觉】双目测距(五)--匹配算法对比
查看>>
KMP模板
查看>>