High-efficiency cropped picture fillet algorithm for iOS

Source: Internet
Author: User

There is a need for the project: crop the picture, for the Avatar, here are the requirements:

You can see that the fillet of this picture has been removed, here's what I've done in the project two ways to achieve this cropping and to see how the technical documentation found more efficient cropping, the following one by one explains: It takes about 15-20 minutes to see it.

To create a class method in a Util class in a public class

1.CGContext cropping

// Cgcontext clipping + (UIImage *) Cgcontextclip: (UIImage *) img Cornerradius: (cgfloat) C;

Implement this method:

//Cgcontext cropping+ (UIImage *) Cgcontextclip: (UIImage *) img Cornerradius: (cgfloat) c{intw = img.size.width *Img.scale; intH = img.size.height *Img.scale; Uigraphicsbeginimagecontextwithoptions (Cgsizemake (W, h),false,1.0); Cgcontextref Context=Uigraphicsgetcurrentcontext (); Cgcontextmovetopoint (Context,0, c); Cgcontextaddarctopoint (Context,0,0C0, c); Cgcontextaddlinetopoint (Context, W-C,0); Cgcontextaddarctopoint (Context, W,0, W, C, c); Cgcontextaddlinetopoint (Context, W, H-B); Cgcontextaddarctopoint (Context, W, H, W-C, H, c);    Cgcontextaddlinetopoint (context, C, h); Cgcontextaddarctopoint (Context,0H0, H-c, c); Cgcontextaddlinetopoint (Context,0, c);         Cgcontextclosepath (context); //Crop the context first, then draw it, and you'll be drawn in the cropped path .Cgcontextclip (context); [img Drawinrect:cgrectmake (0,0, W, h)];//DrawingCgcontextdrawpath (context, Kcgpathfill); UIImage*ret =Uigraphicsgetimagefromcurrentimagecontext ();        Uigraphicsendimagecontext (); returnret;}

The following is called where it is needed:

[Util cgcontextclip:image Cornerradius:radius];

2.UIBezierPath cropping

Declaring in the Util.h class

// Uibezierpath clipping + (UIImage *) Uibezierpathclip: (UIImage *) img Cornerradius: (cgfloat) C;

Implementation methods in UTIL.M

//Uibezierpath cropping+ (UIImage *) Uibezierpathclip: (UIImage *) img Cornerradius: (cgfloat) c{intw = img.size.width *Img.scale; intH = img.size.height *Img.scale; CGRect rect= CGRectMake (0,0, W, h); Uigraphicsbeginimagecontextwithoptions (Cgsizemake (W, h),false,1.0);    [[Uibezierpath Bezierpathwithroundedrect:rect cornerradius:c] addclip];        [img Drawinrect:rect]; UIImage*ret =Uigraphicsgetimagefromcurrentimagecontext ();    Uigraphicsendimagecontext (); returnret;}

3. Airspace processing method, write an algorithm for clipping fillet

For a point on the image (x, y), determine that it is not within the rounded rectangle, where alpha is the original value, and if not, Alpha is set to 0.

Traverse all pixels, judging that each pixel is within a circle that is not 4 circles, 4 corners, and each corner has a one-fourth circle.

An optimization is that I do not need to traverse all the pixels to be able to cut out the fillet, just to consider the lower left corner of the Triangle area on the line, left, left, upper right, right, a total of 4 triangular regions (another 3 are not drawn), for the Loop, Just loop over this 4 triangle area on the line.

So for a W * H image, the fillet size is n,n <= min (w, h)/2, its complexity is O (n) = 2 (n^2), and the worst case calculation will not exceed WH/2.

For a pixel (x, y), determine its formula within the circle:
if (X-CX) ^2 + (y-cy) ^2 <= r^2 represents the point (x, y) within the circle, and vice versa. Pass test: This algorithm efficiency can be increased several times above (time)

In the Util.h, declare:

+ (UIImage *) Dealimage: (UIImage *) img Cornerradius: (cgfloat) C

Implemented in UTIL.M:

+ (UIImage *) Dealimage: (UIImage *) img Cornerradius: (cgfloat) C {//1.CGDataProviderRef turn cgimage into binary streamCgdataproviderref Provider =Cgimagegetdataprovider (img.    Cgimage); void*imgdata = (void*) Cfdatagetbyteptr (Cgdataprovidercopydata (provider)); intwidth = img.size.width *Img.scale; intHeight = img.size.height *Img.scale; //2. Handling Imgdata//dealimage (imgdata, width, height);cornerimage (imgdata, width, height, c); //3.CGDataProviderRef binary Flow cgimageCgdataproviderref PV = Cgdataprovidercreatewithdata (NULL, imgdata, Width * Height *4, Releasedata); Cgimageref content= Cgimagecreate (width, height,8, +,4* Width, Cgcolorspacecreatedevicergb (), Kcgbitmapbyteorder32big | Kcgimagealphapremultipliedlast, PV, NULL,true, Kcgrenderingintentdefault); UIImage*result =[UIImage imagewithcgimage:content];      Cgdataproviderrelease (PV); //Free Spacecgimagerelease (content); returnresult;}voidReleasedata (void*info,Const void*data, size_t size) {Free (void*) data);}//process pictures on IMG, test withvoidDealimage (UInt32 *img,intWinth) {intnum = w *h; UInt32*cur =img;  for(intI=0; i<num; i++, cur++) {UInt8*p = (UInt8 *) cur; //RGBA Arrangement//f (x) = 255-g (x) for negativep[0] =255-p[0]; p[1] =255-p[1]; p[2] =255-p[2]; p[3] =255; }}//Crop rounded cornersvoidCornerimage (UInt32 *ConstImgintWinth, CGFloat Cornerradius) {cgfloat C=Cornerradius; CGFloat min= w > h?h:w; if(C <0) {c =0; } if(C > Min *0.5) {c = min *0.5; } //Upper left Y:[0, c), X:[x, c-y)     for(inty=0; y<c; y++) {         for(intx=0; x<c-y; X + +) {UInt32*p = img + y * w + x;//p 32-bit pointer, RGBA array, 8 bits each            if(Iscircle (c, C, C, x, y) = =false) {                *p =0; }        }    }    //Upper Right y:[0, c), X:[w-c+y, W)    intTMP = WC;  for(inty=0; y<c; y++) {         for(intX=tmp+y; x<w; X + +) {UInt32*p = img + y * w +x; if(Iscircle (W-c, C, C, x, y) = =false) {                *p =0; }        }    }    //Lower left Y:[h-c, h), x:[0, Y-h+c)TMP = hC;  for(intY=h-c; y) {         for(intx=0; x<y-tmp; X + +) {UInt32*p = img + y * w +x; if(Iscircle (c, H-c, C, x, y) = =false) {                *p =0; }        }    }    //Lower right y~[h-c, h), X~[w-c+h-y, W)TMP = w-c+h;  for(intY=h-c; y) {         for(intX=tmp-y; x<w; X + +) {UInt32*p = img + y * w +x; if(Iscircle (W-c, H-c, C, x, y) = =false) {                *p =0; }        }    }}//Point of Judgment (px, py) within a circle that is not in the radius r of the center (CX, CY)StaticInlineBOOLIscircle (floatCxfloatCyfloatRfloatPxfloatpy) {    if((PX-CX) * (PX-CX) + (py-cy) * (py-cy) > R *r) {return false; }    return true;}//Other image effects can write their own functions, and then call Otherimage in Dealimage:voidOtherimage (UInt32 *ConstImgintWinth) {//Custom Processing}

There are three ways to solve the need for picture clipping,

High-efficiency cropped picture fillet algorithm for iOS

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.