IOS settings Picture ImageView rounded--crop the picture

Source: Internet
Author: User

Previously set picture fillet always set ImageView to a circle, and then set Masktobounds to Yes, in fact, this processing is very expensive performance, the picture after the comparison card, it is best to cut the picture after the display; Here is a classification can be used:

Uiimage+wiroundedrectimage.h

#import <UIKit/UIKit.h> @interface UIImage (wiroundedrectimage) + (ID) createroundedrectimage: (uiimage*) image Size: (cgsize) Size radius: (nsinteger) R; @end

Uiimage+wiroundedrectimage.m

#import "Uiimage+wiroundedrectimage.h" @implementation UIImage (wiroundedrectimage) static void Addroundedrecttopath (        Cgcontextref context, CGRect rect, float ovalwidth, float ovalheight) {float FW, FH;        if (Ovalwidth = = 0 | | ovalheight = = 0) {cgcontextaddrect (context, rect);    Return    } cgcontextsavegstate (context);    CGCONTEXTTRANSLATECTM (Context, Cgrectgetminx (rect), Cgrectgetminy (rect));    CGCONTEXTSCALECTM (context, ovalwidth, ovalheight);    FW = Cgrectgetwidth (rect)/ovalwidth;        FH = cgrectgetheight (rect)/ovalheight;  Cgcontextmovetopoint (Context, FW, FH/2);  Start at lower right corner cgcontextaddarctopoint (context, FW, FH, FW/2, FH, 1); Top Right corner cgcontextaddarctopoint (context, 0, FH, 0, FH/2, 1); Top left corner cgcontextaddarctopoint (context, 0, 0, FW/2, 0, 1); Lower left corner Cgcontextaddarctopoint (context, FW, 0, FW, FH/2, 1); Back to lower right cgconteXtclosepath (context); Cgcontextrestoregstate (context);} + (ID) createroundedrectimage: (uiimage*) Image size: (cgsize) Size radius: (nsinteger) r{//the size of Cgcontextref int    W = size.width;        int h = size.height;    UIImage *img = image;    Cgcolorspaceref colorspace = Cgcolorspacecreatedevicergb ();    Cgcontextref context = Cgbitmapcontextcreate (NULL, W, H, 8, 4 * W, colorspace, Kcgimagealphapremultipliedfirst);        CGRect rect = CGRectMake (0, 0, W, h);    Cgcontextbeginpath (context);    Addroundedrecttopath (context, Rect, R, R);    Cgcontextclosepath (context);    Cgcontextclip (context); Cgcontextdrawimage (context, CGRectMake (0, 0, W, h), IMG.    Cgimage);    Cgimageref imagemasked = cgbitmapcontextcreateimage (context);        IMG = [UIImage imagewithcgimage:imagemasked];    Cgcontextrelease (context);    Cgcolorspacerelease (ColorSpace);        Cgimagerelease (imagemasked); return img;} @end

Call Method:

1UIImage * image = [uiimageimagenamed:@"123.jpg"];//set the original2 3Cgsize size = Cgsizemake ( -, -);//Set Dimensions4 5Uiimageview *testimageview =[[Uiimageview alloc] init];6 7Testimageview.frame = CGRectMake ( -, -, ImageWidth, imagewidth);8 9Testimageview.backgroundcolor =[Uicolor Lightgraycolor];Ten  OneTestimageview.contentmode =Uiviewcontentmodescaleaspectfit; A  - [Self.view Addsubview:testimageview]; -  theTestimageview.image = [Uiimagecreateroundedrectimage:image size:size radius:Ten];//Set Radius

Code Demo:http://files.cnblogs.com/files/a--g/roundimagedemo.zip

In fact, there's a library on GitHub that provides multiple processing of the image:

Uiimage+resize Resizing a picture
Github:https://github.com/coryalder/uiimage_resize
There are a number of ways to set transparency, fillet, crop, resize, and so on for a picture:

1-(UIImage *) Imagewithalpha;2-(UIImage *) Transparentborderimage: (Nsuinteger) bordersize;3-(UIImage *) Roundedcornerimage: (Nsinteger) cornersize bordersize: (Nsinteger) bordersize;4-(UIImage *) Croppedimage: (CGRect) bounds;5-(UIImage *) Thumbnailimage: (Nsinteger) thumbnailsize6 Transparentborder: (nsuinteger) bordersize7 Cornerradius: (nsuinteger) Cornerradius8 interpolationquality: (cginterpolationquality) quality;9-(UIImage *) Resizedimage: (cgsize) newSizeTen interpolationquality: (cginterpolationquality) quality; One-(UIImage *) A Resizedimagewithcontentmode: (uiviewcontentmode) Contentmode - bounds: (cgsize) Bounds -Interpolationquality: (cginterpolationquality) quality;

For more detailed use see: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

Reference Links: 1. Http://www.cnblogs.com/thefeelingofsimple/archive/2013/02/20/2918547.html

2. http://www.cnblogs.com/A--G/p/4779759.html

IOS settings Picture ImageView rounded--crop the picture

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.