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