iOS开发中 UIImage可能经常需要转为NSData 上传 传递等等
有两个比较常用的方法
- UIImageJPEGRepresentation
- UIImagePNGRepresentation
第一个方法有两个参数
- UIImageJPEGRepresentation(UIImage * __nonnull image, CGFloat compressionQuality);
第一个参数就是图片,第二个参数 就是需要设置的清晰度,值介于0到1之前,值越大却清晰,占用空间就越大,反之,值越小清晰度就越低,占用空间也就越小
我们直接上代码
吧两种方式做一个比较
我们创建一个项目,调用系统的相册或者照相机 取一张图片
先用第一种方式转换一个清晰度较高的图片(此处我们贴出重要代码,其他代码我们会上传到qq群空间,请见文章底部)
- UIImage *image;
- image=[info valueForKey:UIImagePickerControllerOriginalImage];
- imageData = UIImageJPEGRepresentation(image,1);
- float length = [imageData length]/1024;
- NSLog(@"image length===%f",length);
在用第一个方式转一个清晰度较低的图片
- imageData1 = UIImageJPEGRepresentation(image,0.1);
- float length1 = [imageData1 length]/1024;
- NSLog(@"image length1===%f",length1);
再用第二种方法转换一张
- imageData2 = UIImagePNGRepresentation(image);
- float length2 = [imageData2 length]/1024;
- NSLog(@"image length===%f",length2);
我们通过执行代码 会发现
- TestImageToData[1317:494705] image length===297.000000
- TestImageToData[1317:494705] image length1===34.000000
- TestImageToData[1317:494705] image length===190.000000
同一张图片 通过三种方式转换后 大小差别比较打
第一种方式 高质量图片297Kb 低质量图片34Kb
第二种方式出来的190Kb
=================
//sdk中提供了方法可以直接调用
UIImage *img = [UIImage imageNamed:@"some.png"];
NSData *dataObj = UIImageJPEGRepresentation(img, 1.0);
//下面是sdk中UIImage.h头文件中的内容
UIKIT_EXTERN NSData *UIImagePNGRepresentation(UIImage *image);
// return image as PNG. May return nil if image has no CGImageRef or invalid bitmap format
UIKIT_EXTERN NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);
// return image as JPEG. May return nil if image has no CGImageRef or invalid bitmap format. compression is 0(most)..1(least)
JPEG的转换方法里面第二个参数是压缩系数,可以有效的减小图片的大小。UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImage* image, 1.0) 返回的图片数据量大很多。项目中做图片上传之前,经过测试同一张拍照所得照片png大小在8M,而JPG压缩系数为0.75时候,大小只有1M。而且,将压缩系数降低对图片视觉上并没有太大的影响。
==========================
利用UIImageJPEGRepresentation与UIGraphicsBeginImageContext进行图片压缩的简单比较
UIImage *image01 = [UIImage imageNamed:@
"002.png"
];
//原图
02.
03.
NSData *dataOf02 =UIImageJPEGRepresentation(image01,
0.5
);
//压缩图片内容,不影响图片的size,得到一个原大小,但更模糊的图片。
04.
UIImage *image02 = [UIImage imageWithData:dataOf02];
//压缩图02
05.
06.
UIImage *image03 = [self makeThumbnailFromImage:image01 scale:
0.5
];
//压缩图03
07.
NSLog(@
"001===%@"
,NSStringFromCGSize(image01.size));
08.
NSLog(@
"002===%@"
,NSStringFromCGSize(image02.size));
09.
NSLog(@
"003===%@"
,NSStringFromCGSize(image03.size));
10.
11.
NSData *data001 = UIImageJPEGRepresentation(image01,
1.0
);
12.
NSData *data002 = UIImageJPEGRepresentation(image02,
1.0
);
13.
NSData *data003 = UIImageJPEGRepresentation(image03,
1.0
);
14.
15.
NSLog(@
"002.lenght===%d"
,data001.length);
16.
NSLog(@
"002.lenght===%d"
,data002.length);
17.
NSLog(@
"003.lenght===%d"
,data003.length);
18.
19.
//看一下效果
20.
UIImageView *imageView01 = [[UIImageView alloc] initWithFrame:CGRectMake(
0
,
0
, image01.size.width, image01.size.height)];
21.
imageView01.image = image01;
22.
[self.view addSubview:imageView01];
23.
24.
UIImageView *imageView02 = [[UIImageView alloc] initWithFrame:CGRectMake(
0
,
100
, image02.size.width, image02.size.height)];
25.
imageView02.image = image02;
26.
[self.view addSubview:imageView02];
27.
28.
UIImageView *imageView03 = [[UIImageView alloc] initWithFrame:CGRectMake(
0
,
200
, image03.size.width, image03.size.height)];
29.
imageView03.image = image03;
30.
[self.view addSubview:imageView03];
01.
//手动实现图片压缩,可以写到分类里,封装成常用方法。按照大小进行比例压缩,改变了图片的size。
02.
- (UIImage *)makeThumbnailFromImage:(UIImage *)srcImage scale:(
double
)imageScale {
03.
UIImage *thumbnail = nil;
04.
CGSize imageSize = CGSizeMake(srcImage.size.width * imageScale, srcImage.size.height * imageScale);
05.
if
(srcImage.size.width != imageSize.width || srcImage.size.height != imageSize.height)
06.
{
07.
UIGraphicsBeginImageContext(imageSize);
08.
CGRect imageRect = CGRectMake(
0.0
,
0.0
, imageSize.width, imageSize.height);
09.
[srcImage drawInRect:imageRect];
10.
thumbnail = UIGraphicsGetImageFromCurrentImageContext();
11.
UIGraphicsEndImageContext();
12.
}
13.
else
14.
{
15.
thumbnail = srcImage;
16.
}
17.
return
thumbnail;
18.
}