We often need to cut some of the pictures are not round and then use, such as QQ Head, Weibo avatar, etc. are round, then the question came, how to put a picture is not a circular cut into a round it? Here is one of the ways to achieve this requirement: the concrete implementation of the idea: 1. Assume that the border width is BorderW2. The size of the open picture context should be twice times the width of the original image plus the BORDERW, so that it is opened so that the original image is not deformed. 3. Add a circular fill path above the context. Position starting from 0, 0 o'clock, The width is as large as the context size. Sets the color of the border to be set. 4. Continue to add a circular path above the context, which is the clipping path. It's X, Y start with the borderw point. The width and height are the same as the width of the original picture. Sets the drawn path to the clipping area. 5. Draw the original path into the context. The position of the drawing is the same as the position of the cropping area, X, Y draw from border. 6. Remove the picture from the context State. 7. Close the context state.
3.gif load the picture to be cropped UIImage *image = [UIImage imagenamed:@ "Ali Avatar"]; 0. Set the border size. CGFloat borderw = 10; 1. Open a bitmap context that is the same size as the original image. Cgsize size = Cgsizemake (image.size.width + 2 *borderw, Image.size.height + 2 * borderw); Uigraphicsbeginimagecontextwithoptions (size,no,0); 2. Draw a great circle, fill uibezierpath *path = [Uibezierpath bezierpathwithovalinrect:cgrectmake (0, 0, Size.width, size.height)];
[[Uicolor bluecolor] set]; [path fill]; 3. Add a cropping area . Path = [Uibezierpath bezierpathwithovalinrect:cgrectmake (Borderw, Borderw, Image.size.width, Image.size.height )]; [path addclip]; 4. Draw the picture into the clipping area . [Image Drawatpoint:cgpointmake ( BORDERW, Borderw)]; 5. Generate a new picture . UIImage *clipimage = Uigraphicsgetimagefromcurrentimagecontext (); 6. Close context . uigraphicsendimagecontext (); Extraction Classification Method: Creates a rounded picture with a border, based on the incoming image. Borderw Border width bordercolor: border color Image: The original picture to be generated. + (UIImage *) imagewithborderw :(cgfloat) borderw bordercolor: (uicolor *) color Image: (UIImage *) image; + (UIImage *) Imagewithborderw: (cgfloat) Borderw bordercolor: (uicolor *) color Image: (UIImage *) image{ 1. Open a bitmap context that is the same size as the original picture . Cgsize size = Cgsizemake (image.size.width + 2 *borderw, Image.size.height + 2 * borderw); UigraphiCsbeginimagecontextwithoptions (size,no,0); 2. Draw a great circle, fill uibezierpath *path = [ Uibezierpath bezierpathwithovalinrect:cgrectmake (0, 0, Size.width, size.height)];
[[Uicolor Bluecolor] set]; [Path fill]; 3. Add a clipping area. Path = [Uibezierpath bezierpathwithovalinrect:cgrectmake (Borderw, Borderw, Image.size.width, Image.size.height)]; [Path Addclip]; 4. Draw the picture into the clipping area. [Image Drawatpoint:cgpointmake (Borderw, BORDERW)]; 5. Create a new picture. UIImage *clipimage = Uigraphicsgetimagefromcurrentimagecontext (); 6. Close the context. Uigraphicsendimagecontext ();
return clipimage;
}
Circular picture clipping with borders on iOS