iOS compressed picture size

Source: Internet
Author: User

Recently encountered a more stupid question, the project to do in the photo or photo album selection from the upload, not processed, directly to the original upload, resulting in the list, when it is clear that the small picture traffic is going to explode, think of the iphone shot size can be in m units. So hurriedly do the next compression processing and then upload. In order to facilitate the invocation of different compression requirements, it is more flexible to call the method of modifying parameters. The method to invoke is as follows:

Picture scaling to a specified size-(uiimage*) Imagebyscalingandcroppingforsize: (cgsize) targetsize forimage: (UIImage *) originimage{    UIImage *sourceimage = originimage;//original UIImage *newimage = nil;//New Figure//original size cgsize imageSize = sourceimage.size;    CGFloat width = imagesize.width;    CGFloat height = imagesize.height; CGFloat targetwidth = targetsize.width;//target width cgfloat targetheight = targetsize.height;//target height//scaling parameter initialization CGFloat SC    Alefactor = 0.0;    CGFloat scaledwidth = targetwidth;    CGFloat scaledheight = targetheight;        Cgpoint thumbnailpoint = Cgpointmake (0.0,0.0); if (Cgsizeequaltosize (imageSize, targetsize) = = NO) {//If the original size differs from the target size cgfloat widthfactor = Targetwidth/widt        H                CGFloat heightfactor = targetheight/height; if (Widthfactor > Heightfactor) scalefactor = Widthfactor; Scaling by width else scalefactor = heightfactor;        According to the height telescopic scaledwidth= width * scalefactor; Scaledheight = height * SCALEFACTor  Locate the center point of the picture if (Widthfactor > Heightfactor) {thumbnailpoint.y = (targetheight-scaledheight)        * 0.5;         } else if (Widthfactor < heightfactor) {thumbnailpoint.x = (targetwidth-scaledwidth) * 0.5;    }}//Create a bitmap-based context Uigraphicsbeginimagecontext (targetsize);    Target size CGRect thumbnailrect = Cgrectzero;    Thumbnailrect.origin = Thumbnailpoint;    Thumbnailrect.size.width= Scaledwidth;        ThumbnailRect.size.height = Scaledheight;    [Sourceimage Drawinrect:thumbnailrect];    New Picture NewImage = Uigraphicsgetimagefromcurrentimagecontext ();        if (NewImage = nil) NSLog (@ "Could not scale image");    Exits the bitmap context uigraphicsendimagecontext (); return newimage;}

I have written it in the comments. This method accepts two parameters, one is the size to be compressed, and the other is the original. The process of invoking is also very simple, such as:

Telescopic picture if ([self imagebyscalingandcroppingforsize:cgsizemake (+) Forimage:theimage]) {//Telescopic success    theimage = [ Self Imagebyscalingandcroppingforsize:cgsizemake (+) forimage:theimage];}

This is OK, I put the picture file named Theimage compression to the size of 100*100, after testing, the original 1M image processing only more than 20 k, so the flow of pressure is greatly reduced.

This method can also be used to stretch the size of the picture, but I still used to compress, in the case of not pursuing high definition, the use is very convenient and flexible, I hope to help everyone ~


All rights reserved: Http://blog.csdn.net/cloudox_

iOS compressed picture size

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.