UIImageJPEGRepresentation方法在耗时上比较少 而UIImagePNGRepresentation耗时操作时间比较长
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
使用UIImagePNGRepresentation取得照片时候可能会造成卡顿的现象在Iphone上有两种读取图片数据的简单方法: UIImageJPEGRepresentation和UIImagePNGRepresentation.
UIImageJPEGRepresentation函数需要两个参数:图片的引用和压缩系数.而UIImagePNGRepresentation只需要图片引用作为参数.通过在实际使用过程中,比较发现: UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImage* image, 1.0) 返回的图片数据量大很多.譬如,同样是读取摄像头拍摄的同样景色的照片, UIImagePNGRepresentation()返回的数据量大小为199K ,而 UIImageJPEGRepresentation(UIImage* image, 1.0)返回的数据量大小只为140KB,比前者少了50多KB.如果对图片的清晰度要求不高,还可以通过设置 UIImageJPEGRepresentation函数的第二个参数,大幅度降低图片数据量.譬如,刚才拍摄的图片, 通过调用UIImageJPEGRepresentation(UIImage* image, 1.0)读取数据时,返回的数据大小为140KB,但更改压缩系数后,通过调用UIImageJPEGRepresentation(UIImage* image, 0.5)读取数据时,返回的数据大小只有11KB多,大大压缩了图片的数据量 ,而且从视角角度看,图片的质量并没有明显的降低.因此,在读取图片数据内容时,建议优先使用UIImageJPEGRepresentation,并可根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小.
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
- [formatter setDateFormat:@"YYYY-MM-DD-hh-mm-ss"];
- if (UIImagePNGRepresentation(image)==nil) {
- data = UIImageJPEGRepresentation(image, 1.0);
- }else{
- data = UIImagePNGRepresentation(image);
- }
利用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.
}
效果图: