IOS Crop pictures proportionally

Source: Internet
Author: User

Take photos or get pictures from the picture library in the process of easy to flash back, there will always be a memory pressure warning, the first step, you can first consider cutting the picture, in fact, may not need that big. Second, consider putting time-consuming such as stored procedures into threads.

This encapsulates the class method of cropping the picture.

navview.m

#define IMAGE_MAX_SIZE_WIDTH 640

#define Image_max_size_geight 1136

+ (UIImage *) Fitsmallimage: (UIImage *) image{if(Nil = =image) {        returnNil; }    if(Image.size.width<image_max_size_width && image.size.height<image_max_size_geight) {        returnimage; } cgsize Size= [self fitsize:image.size];    Uigraphicsbeginimagecontext (size); CGRect rect= CGRectMake (0,0, Size.width, size.height);    [Image Drawinrect:rect]; UIImage*newing =Uigraphicsgetimagefromcurrentimagecontext ();    Uigraphicsendimagecontext (); returnnewing;}+ (cgsize) Fitsize: (cgsize) thissize {    if(Thissize.width = =0&& Thissize.height = =0)        returnCgsizemake (0,0); CGFloat Wscale= thissize.width/Image_max_size_width; CGFloat Hscale= thissize.height/Image_max_size_geight; CGFloat Scale= (Wscale>hscale)?Wscale:hscale; Cgsize newSize= Cgsizemake (Thissize.width/scale, thissize.height/Scale ); returnNewSize;}
Using eg:
Set the image to be manipulated as image(uiimage)
Image=[navview
Fitsmallimage:Image];

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.