NSData *imageData =
UIImageJPEGRepresentatio n
(self.uploadImage , 0.75);
//图片按0.75的质量压缩-》转换为NSData
NSString *file_name = [TimeUtil getDateStringWithFormat: @"yyyy-MM-dd-HH-mm-ss"];
//以事件为图片名字
NSString *upload_name =[NSString stringWithFormat:@"%@.jpg",file_name ];
//上传的图片名字
[request setData: imageData
withFileName:
upload_name
andContentType:
@"image/jpeg"
forKey:
@"upload_file"];
UIImageJPEGRepresentatio n 函数需要两个参数:图片的引用和压缩系数.
而UIImagePNGRepresentation 只需要图片引用作为参数.通过在实际使用过程中,
比较发 现:
UIImagePNGRepresentation (UIImage* image) 要比UIImageJPEGRepresentatio n(UIImage* image, 1.0) 返回的图片数据量大很多.
譬如,同样是读取摄像头拍摄的同样景色的照片
UIImagePNGRepresentation ()返回的数据量大小为199K
而 UIImageJPEGRepresentatio n(UIImage* image, 1.0)返回的数据量大小只为140KB
比前者少了50多KB.
如果对图片的清晰度要求不高,还可以通过设置 UIImageJPEGRepresentatio n函数的第二个参数,大幅度降低图片数据量.
譬如,刚才拍摄的图片, 通过调用UIImageJPEGRepresentatio n(UIImage* image, 1.0)读取数据时,返回的数据大小为140KB
但更改压缩系数后,通过调用UIImageJPEGRepresentatio n(UIImage* image, 0.5)读取数据时,返回的数据大小只有11KB多,大大压缩了图片的数据量
而且从视角角度看,图片的质量并没有明显的降低.因此,在读取图片数据内容时,建议优先使用UIImageJPEGRepresentatio n,并可 根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小.
//图片按0.75的质量压缩-》转换为NSData
NSString *file_name = [TimeUtil getDateStringWithFormat: @"yyyy-MM-dd-HH-mm-ss"];
//以事件为图片名字
NSString *upload_name =[NSString stringWithFormat:@"%@.jpg",file_name ];
//上传的图片名字
[request setData: imageData
UIImageJPEGRepresentatio
而UIImagePNGRepresentation
比较发 现:
UIImagePNGRepresentation
譬如,同样是读取摄像头拍摄的同样景色的照片
比前者少了50多KB.
如果对图片的清晰度要求不高,还可以通过设置 UIImageJPEGRepresentatio
譬如,刚才拍摄的图片, 通过调用UIImageJPEGRepresentatio
但更改压缩系数后,通过调用UIImageJPEGRepresentatio