IOS image compression UIImage method extension

Source: Internet
Author: User

http://blog.csdn.net/justinjing0612/article/details/8751269

iOS comes with an API that provides the following

[HTML]View Plaincopy
    1. NSData *uiimagejpegrepresentation (UIImage *image, CGFloat compressionquality);

There are two simple ways to read image data on the iphone: UIImageJPEGRepresentation and Uiimagepngrepresentation. The UIImageJPEGRepresentation function requires two parameters: a reference to the picture and a compression factor. Instead, uiimagepngrepresentation only requires a picture reference as a parameter. Through the actual use of the process, the comparison found: Uiimagepngrepresentation (uiimage* image) is much larger than the amount of image data returned by uiimagejpegrepresentation (uiimage* image, 1.0). For example, The same picture that reads the same view taken by the camera, Uiimagepngrepresentation () returns a data volume of 199K, while UIImageJPEGRepresentation (uiimage* image, 1.0) The amount of data returned is only 140KB, which is less than 50 KB. If the sharpness of the picture is not required, you can also reduce the amount of the image data by setting the second parameter of the UIImageJPEGRepresentation function. For example, the picture just taken, When reading data by calling UIImageJPEGRepresentation (uiimage* image, 1.0), the returned data size is 140KB, but after changing the compression factor, by calling UIImageJPEGRepresentation ( uiimage* image, 0.5) when reading data, the size of the returned data is only more than 11KB, greatly compressing the amount of data in the image, and from the angle of view, the quality of the picture is not significantly reduced. Therefore, when reading the picture data content, It is recommended to use UIImageJPEGRepresentation first, and set the compression factor according to your actual usage scene, further reduce the size of image data.

[HTML]View Plaincopy < param name= "wmode" value= "Transparent" >
    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 Specific Code

[HTML]View Plaincopy < param name= "wmode" value= "Transparent" >
    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 specific Code

[HTML]View Plaincopy
    1. #import "UIImageExt.h"
    2. @implementation UIImage (Uiimageext)
    3. -(UIImage *) Scaletosize: (UIImage *) img Size: (cgsize) size{
    4. Create a bitmap context
    5. and set it to the context that is currently being used
    6. Uigraphicsbeginimagecontext (size);
    7. Draw a picture that changes size
    8. [img drawinrect:cgrectmake (0, 0, Size.width, size.height)];
    9. Create a resized picture from the current context
    10. uiimage* scaledimage = uigraphicsgetimagefromcurrentimagecontext ();
    11. Make the current context out of the stack
    12. Uigraphicsendimagecontext ();
    13. Returns the new resized image
    14. return scaledimage;
    15. }
    16. -(uiimage*) Imagebyscalingandcroppingforsize: (cgsize) targetsize
    17. {
    18. UIImage *sourceimage = self ;
    19. UIImage *newimage = nil;
    20. Cgsize imageSize = sourceimage.size;
    21. CGFloat width = imagesize.width;
    22. CGFloat height = imagesize.height;
    23. CGFloat targetwidth = targetsize.width;
    24. CGFloat targetheight = targetsize.height;
    25. CGFloat scalefactor = 0.0;
    26. CGFloat scaledwidth = targetwidth;
    27. CGFloat scaledheight = targetheight;
    28. Cgpoint thumbnailpoint = cgpointmake (0.0,0.0);
    29. if (Cgsizeequaltosize (imageSize, targetsize) = = NO)
    30. {
    31. CGFloat widthfactor = targetwidth/width;
    32. CGFloat heightfactor = targetheight/height;
    33. if (Widthfactor > Heightfactor)
    34. scalefactor = widthfactor;//scale to fit height
    35. Else
    36. scalefactor = heightfactor;//scale to fit width
    37. scaledwidth = width * scalefactor;
    38. scaledheight = Height * scalefactor;
    39. Center the image
    40. if (Widthfactor > Heightfactor)
    41. {
    42. Thumbnailpoint.y = (targetheight-scaledheight) * 0.5;
    43. }
    44. Else
    45. if (Widthfactor < heightfactor)
    46. {
    47. thumbnailpoint.x = (targetwidth-scaledwidth) * 0.5;
    48. }
    49. }
    50. Uigraphicsbeginimagecontext (targetsize); This would crop
    51. CGRect thumbnailrect = Cgrectzero;
    52. thumbnailrect.origin = thumbnailpoint;
    53. thumbnailRect.size.width = scaledwidth;
    54. thumbnailRect.size.height = scaledheight;
    55. [Sourceimage Drawinrect:thumbnailrect];
    56. newimage = uigraphicsgetimagefromcurrentimagecontext ();
    57. if (newimage = = nil)
    58. NSLog (@ "Could not scale image");
    59. Pop the context to get back to the default
    60. Uigraphicsendimagecontext ();
    61. return newimage;
    62. }
    63. @end

IOS image compression UIImage method extension

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.