The example of this article introduces the detailed code of the iOS cropped round head, for everyone's reference, the specific contents are as follows
-(void) viewdidload {
[super viewdidload];
Load Picture
uiimage *image = [UIImage imagenamed:@ "brother Philip"];
Get picture size
cgsize size = image.size;
Open Bitmap Context
uigraphicsbeginimagecontextwithoptions (size, NO, 0);
Create a circular path
uibezierpath *path = [Uibezierpath bezierpathwithovalinrect:cgrectmake (0, 0, Image.size.width, Image.size.height)];
Set to Crop area
[path Addclip];
Draw picture
[Image Drawatpoint:cgpointzero];
Gets the cropped picture
_imageview.image = Uigraphicsgetimagefromcurrentimagecontext ();
Close context
uigraphicsendimagecontext ();
}
One more picture of Brother Phillips.
If you want to have a round head with a border, the idea is to draw a big circle, and then in this circle size range to draw a picture of the size of a circle.
-(void) viewdidload {
[super viewdidload];
Load Picture
uiimage *image = [UIImage imagenamed:@ "Big Brother"];
Set border width
cgfloat border = 3;
CGFloat imagewh = image.size.width;
Calculate the dimensions of the outer circle
cgfloat ovalwh = imagewh + 2 * border;
Open context
uigraphicsbeginimagecontextwithoptions (image.size, NO, 0);
Draw a large circular
uibezierpath *path = [Uibezierpath bezierpathwithovalinrect:cgrectmake (0, 0, OVALWH, OVALWH)];
[[Uicolor Orangecolor]set];
[path fill];
Set Crop area
uibezierpath *path1 = [Uibezierpath bezierpathwithovalinrect:cgrectmake (Border, border, IMAGEWH, IMAGEWH)];
[path1 Addclip];
Draw pictures
[image drawatpoint:cgpointmake (Border, border)];
Gets the picture from the context
_imageview.image = Uigraphicsgetimagefromcurrentimagecontext ();
Close context
uigraphicsendimagecontext ();
}
Effect as shown:
Screen capture code:
The principle is to render the layer of the controls on the screen into context
-(void) viewdidload {
[super viewdidload];
Open context
uigraphicsbeginimagecontextwithoptions (self.view.bounds.size, NO, 0);
Gets the context
cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Renders the layer on the control to the context
[Self.view.layer renderincontext:ctx];
Gets the picture in the context
uiimage *image = Uigraphicsgetimagefromcurrentimagecontext ();
Closes the context
uigraphicsendimagecontext ();
Save picture to album
Uiimagewritetosavedphotosalbum (image, nil, nil, nil);
}
The above is the entire content of this article, I hope to help you learn.