Get rounded pictures using Quartz 2D in IOS development

Source: Internet
Author: User

Background:

Now in social software, the fillet of the picture can be said to be flooding, the original square QQ head, have been the secular grind flat ...

So how do you turn a picture into rounded corners?

You might say, "Look for the art!" "To find art!" But what if the user wants to upload a custom avatar?

Or a program to deal with it!

Below we use the powerful Quartz 2D to process the image ourselves

Directly on the code:

Code:

I have added a classification to UIImage, which is used directly for various

+ (Instancetype) Imagewithicon: (NSString *) icon border: (nsinteger) Border color: (Uicolor *) color{//0. Get the original image UIImage *i        Mage = [UIImage Imagenamed:icon];    1. Create picture context cgfloat margin = border;        Cgsize size = Cgsizemake (image.size.width + margin, image.size.height + margin);    YES Opaque No transparent uigraphicsbeginimagecontextwithoptions (size, no, 0);    2. The large circle Cgcontextref ctx = Uigraphicsgetcurrentcontext ();    Cgcontextaddellipseinrect (CTX, cgrectmake (0, 0, size.width, size.height));    [Color set];        Cgcontextfillpath (CTX);    3. Draw Small Circle CGFloat smallx = margin * 0.5;    CGFloat smally = margin * 0.5;    CGFloat SMALLW = image.size.width;    CGFloat SMALLH = image.size.height;        Cgcontextaddellipseinrect (CTX, CGRectMake (Smallx, smally, SMALLW, SMALLH));        4. Specify the available range, this range is from after this code, i.e., before the unaffected Cgcontextclip (CTX);        5. Drawing pictures [Image Drawinrect:cgrectmake (Smallx, smally, SMALLW, SMALLH)]; 6. Take out the picture UIImage *newimage = uigraphicSgetimagefromcurrentimagecontext ();    return newimage; }

Sorting is done, let's look at the effect:

Original:

Handled well: (Please do not care about my background)


Calling code:


UIImage *image = [UIImage imagewithicon:@ "3.jpg" Border:4 color:[uicolor colorwithred:0 green:0 blue:0 alpha:0.25]];


Note: It is best to make a transparency, otherwise it is very ugly to say.



Get rounded pictures using Quartz 2D in IOS development

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.