iOS压缩图片处理
来源:互联网 发布:mac a1181 t7200 编辑:程序博客网 时间:2024/05/19 02:40
1.压缩图片质量(图片体积减小,像素不变)
两种读取图片数据的简单方法:
(1).UIImageJPEGRepresentation函数需要两个参数:图片的引用和压缩系数,压缩体积不是随压缩系数比例变化的。
(2).UIImagePNGRepresentation只需要图片引用作为参数。
注意:
UIImagePNGRepresentation(image) 要比UIImageJPEGRepresentation(image, 1.0) 返回的图片数据量大很多
建议优先使用 UIImageJPEGRepresentation ,并可根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小。
+(UIImage *)reduceImage:(UIImage *)image percent:(float)percent
{
NSData *imageData =UIImageJPEGRepresentation(image, percent);
UIImage *newImage = [UIImage imageWithData:imageData];
return newImage;
}
2.图片压缩到指定尺寸
+ (UIImage *) scaleImage:(UIImage *) image withNewSize:(CGSize) newSize
{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0, newSize.width, newSize.height)];
UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
3.压缩到指定尺寸等比例压缩
-(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;
}elseif(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;
}elseif(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;
}
- iOS图片压缩处理
- iOS图片压缩处理
- iOS压缩图片处理
- iOS图片压缩处理
- iOS图片压缩处理
- iOS图片压缩处理
- iOS图片压缩处理
- iOS图片压缩处理
- iOS图片压缩处理
- iOS图片压缩处理
- ios图片的压缩处理
- iOS - 关于图片压缩处理
- iOS 上传图片压缩处理
- IOS 图片上传处理 图片压缩 图片处理
- IOS 图片上传处理 图片压缩 图片处理
- IOS 图片上传处理 图片压缩 图片处理
- IOS 图片上传处理 图片压缩 图片处理
- IOS 图片上传处理 图片压缩 图片处理
- iOS面试题 AFN和ASI框架的区别
- iOS面试题 AFN和ASI框架的区别
- iOS9下更新项目遇到的问题
- 运行时的理解
- iOS网络请求的错误状态码
- iOS压缩图片处理
- iOS中POST上传文件的实现
- AFN上传文件和检测网络状态
- KVO内部的实现原理
- GCD的相关知识
- 单例的两种写法
- iOS中block的使用注意
- 深复制和浅复制的概念
- iOS中程序代码存储区块