iOS开发之图片压缩实现
使用下面两个方法,先按尺寸重绘图片,然后再降低品质上传图片data
#pragma mark 裁剪照片 -(UIImage *)scaleToSize:(UIImage *)image size:(CGSize)size { //创建一个bitmap的context //并把他设置成当前的context UIGraphicsBeginImageContext(size); //绘制图片的大小 [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; //从当前context中创建一个改变大小后的图片 UIImage *endImage=UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return endImage; } - (void)printImageDataLength:(UIImage *)image { for (int i=1; i<= 10; i++) { NSData *data = UIImageJPEGRepresentation(image, i * 0.1); NSLog(@"JPEGdata.length%.1lf = %d",i * 0.1,data.length); } NSData *data = UIImagePNGRepresentation(image); NSLog(@"PNGdata.length = %d",data.length); }
UIImage的 + (UIImage *)imageWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation方法