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