摘要:本文将带你了解iOS 开发之图片压缩UIImage方法扩展,希望本文对大家学IOS有所帮助。
iOS自带的提供了一个API如下
[objc] view plain copy 1. NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality); 在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,并可根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小。 [objc] view plain copy 1. UIImage *imageNew = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 2. imageNew = [self imageWithImage:imageNew scaledToSize:CGSizeMake(100, 100)]; 3. NSData *imageData = UIImageJPEGRepresentation(imageNew, 0.0001); 4. m_selectImage = [UIImage imageWithData:imageData]; .h具体code [objc] view plain copy 1. #import <Foundation/Foundation.h> 2. @interface UIImage (UIImageExt) 3. - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size; //图片压缩到指定尺寸 4. - (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize; //压缩到指定尺寸等比例压缩 5. @end .m具体code [objc] view plain copy 1. #import "UIImageExt.h" 2. @implementation UIImage (UIImageExt) 3. 4. - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{ 5. // 创建一个bitmap的context 6. // 并把它设置成为当前正在使用的context 7. UIGraphicsBeginImageContext(size); 8. // 绘制改变大小的图片 9. [img drawInRect:CGRectMake(0, 0, size.width, size.height)]; 10. // 从当前context中创建一个改变大小后的图片 11. UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 12. // 使当前的context出堆栈 13. UIGraphicsEndImageContext(); 14. // 返回新的改变大小后的图片 15. return scaledImage; 16. } 17. - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize 18. { 19. UIImage *sourceImage = self; 20. UIImage *newImage = nil; 21. CGSize imageSize = sourceImage.size; 22. CGFloat width = imageSize.width; 23. CGFloat height = imageSize.height; 24. CGFloat targetWidth = targetSize.width; 25. CGFloat targetHeight = targetSize.height; 26. CGFloat scaleFactor = 0.0; 27. CGFloat scaledWidth = targetWidth; 28. CGFloat scaledHeight = targetHeight; 29. CGPoint thumbnailPoint = CGPointMake(0.0,0.0); 30. 31. if (CGSizeEqualToSize(imageSize, targetSize) == NO) 32. { 33. CGFloat widthFactor = targetWidth / width; 34. CGFloat heightFactor = targetHeight / height; 35. 36. if (widthFactor > heightFactor) 37. scaleFactor = widthFactor; // scale to fit height 38. else 39. scaleFactor = heightFactor; // scale to fit width 40. scaledWidth = width * scaleFactor; 41. scaledHeight = height * scaleFactor; 42. 43. // center the image 44. if (widthFactor > heightFactor) 45. { 46. thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 47. } 48. else 49. if (widthFactor < heightFactor) 50. { 51. thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; 52. } 53. } 54. 55. UIGraphicsBeginImageContext(targetSize); // this will crop 56. 57. CGRect thumbnailRect = CGRectZero; 58. thumbnailRect.origin = thumbnailPoint; 59. thumbnailRect.size.width = scaledWidth; 60. thumbnailRect.size.height = scaledHeight; 61. 62. [sourceImage drawInRect:thumbnailRect]; 63. 64. newImage = UIGraphicsGetImageFromCurrentImageContext(); 65. if(newImage == nil) 66. NSLog(@"could not scale image"); 67. 68. //pop the context to get back to the default 69. UIGraphicsEndImageContext(); 70. return newImage; 71. } 72. @end
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号