Size of modified picture in iOS: Modify resolution and cropping

Source: Internet
Author: User

In the development of iOS, there are often limited image file size, some users picture is very large, resulting in slow upload time, causing problems.

such as: sharing, if the size of the picture appears to be greater than 50kbytes, sharing failed, and no hint.

So, I added two functions:

First, change the size of the current picture, newsize is a new size size, this method to help users get to a smaller picture.

But this newsize size proposal is the same as the original, otherwise the picture will be deformed.

-(UIImage*) Imagewithimagesimple: (UIImage*) image scaledtosize: (cgsize) newSize

{

//Create A graphics image context

uigraphicsbeginimagecontext(newSize);

//Tell the old image to draw in this new context, with the desired

//New size

[Image Drawinrect:cgrectmake(0,0, newSize. Width, newSize. Height)];

//Get The new image from the context

UIImage* newimage = uigraphicsgetimagefromcurrentimagecontext();

//End the context

Uigraphicsendimagecontext();

//Return the new image.

return newimage;

}

Second, the function, realizes the user wants to intercept the graph the rect

-(UIImage *) Getimagebycuttingimage: (UIImage *) image Rect: (cgrect) rect{

// big picture bigimage

// define Thearea of the myimagerect

cgrect myimagerect = rect;

UIImage* bigimage= image;

cgimageref imageref = bigimage. Cgimage;

cgimageref subimageref = cgimagecreatewithimageinrect(Imageref, myimagerect);

cgsize size;

Size. width = rect.size. width;

Size. height = rect.size. Height;

uigraphicsbeginimagecontext(size);

cgcontextref context = uigraphicsgetcurrentcontext();

cgcontextdrawimage(context, myimagerect, subimageref);

UIImage* smallimage = [UIImage imagewithcgimage: subimageref];

Uigraphicsendimagecontext();

return smallimage;

}

Size of modified picture in iOS: Modify resolution and cropping

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.