How IOS cuts round avatar _ios

Source: Internet
Author: User

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.

Related Article

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.