This is the function that I used when I was writing a small experiment. I pulled it out of my own.
+(UIImage *)capturImageWithUIView:(UIView *)view{ //开启位图上下文 UIGraphicsBeginImageContext(view.bounds.size); //获取当前位图 CGContextRef ctx = UIGraphicsGetCurrentContext(); //将视图渲染到位图上 [view.layer renderInContext:ctx]; //获取当前图片 UIImage *currentImage = UIGraphicsGetImageFromCurrentImageContext(); //结束位图编辑 UIGraphicsEndImageContext(); return currentImage;}
Here's what I'm writing. Cut a square image into a circular picture and add a border.
+ (UIImage*)Cutcircleimagewithuiimage: (UIImage*)ImageWithbordercolor: (Uicolor*)ColorWithborderwidth: (cgfloat) width{CGRect imagerect = {{0,0},{Image.size. width,Image.size. Height}};//Open Bitmap contextUigraphicsbeginimagecontextwithoptions (Image.size, NO,0.0);//Get current bitmapCgcontextref CTX = Uigraphicsgetcurrentcontext ();//Specify a circle to delete the part outside the CircleCgcontextaddellipseinrect (CTX, imagerect); Cgcontextclip (CTX);//Add the picture in[ImageDrawinrect:imagerect];//Add a borderCgcontextaddellipseinrect (CTX, imagerect); Cgcontextsetlinewidth (ctx, width); [ColorSet]; Cgcontextstrokepath (CTX);//Get current pictureUIImage*screenimage= Uigraphicsgetimagefromcurrentimagecontext ();//End Bitmap editingUigraphicsendimagecontext ();//Return the picture returnScreenimage;}
File on cloud Disk: Https://yunpan.cn/cP98wIjYCvQAZ (extract code: 30F5)
If you have a good little functional idea, or have suggestions for my code, you can leave a comment in the comments. If you think I can write a little bit of attention, if you can please pay attention to my Weibo: Http://weibo.com/xiaopenguu each update will be updated in the microblog
Thank you for your attention.
iOS development: Capturing the current view