Code to cut an image into rounded corners

Source: Internet
Author: User
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
{
// 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, 10, 10 );
Cgcontextclosepath (context );
Cgcontextclip (context );
Cgcontextdrawimage (context, cgrectmake (0, 0, W, h), IMG. cgimage );
Cgimageref imagemasked = cgbitmapcontextcreateimage (context );
Cgcontextrelease (context );
Cgcolorspacerelstrap (colorspace );
Return [uiimage imagewithcgimage: imagemasked];
}

Directly call createroundedrectimage ....
Returns the rounded corner image.
Modify cgcontextaddarctopoint on the rounded corner ....

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.