IOS开发:截取当前视图

来源:互联网 发布:mac 浏览照片 下一张 编辑:程序博客网 时间:2024/06/13 10:56

这个是我自己在写一个小实验时,用到的功能。被我独立抽取出来

+(UIImage *)capturImageWithUIView:(UIView *)view{    //开启位图上下文    UIGraphicsBeginImageContext(view.bounds.size);    //获取当前位图    CGContextRef ctx = UIGraphicsGetCurrentContext();    //将视图渲染到位图上    [view.layer renderInContext:ctx];    //获取当前图片    UIImage *currentImage = UIGraphicsGetImageFromCurrentImageContext();    //结束位图编辑    UIGraphicsEndImageContext();    return currentImage;}

下面这个是我随手写将方形图片截成圆形图片,并添加边框。

+(UIImage *)cutCircleImageWithUIImage:(UIImage *)image WithBorderColor:(UIColor *)color withBorderWidth:(CGFloat)width{    CGRect imageRect = {{0,0},{image.size.width,image.size.height}};    //开启位图上下文    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);    //获取当前位图    CGContextRef ctx = UIGraphicsGetCurrentContext();    //指定一个圆,将圆以外的部分删掉    CGContextAddEllipseInRect(ctx, imageRect);    CGContextClip(ctx);    //将图片添加进来    [image drawInRect:imageRect];    //添加边框    CGContextAddEllipseInRect(ctx, imageRect);    CGContextSetLineWidth(ctx, width);    [color set];    CGContextStrokePath(ctx);    //获取当前图片    UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();    //结束位图编辑    UIGraphicsEndImageContext();    //将图片返回    return screenImage;}

文件放在云盘中:https://yunpan.cn/cP98wIjYCvQAZ (提取码:30f5)
各位朋友如果有好的小功能想法,或者对我的代码有建议都可以在评论里留言。如果觉得我写的还可以就请您点一下关注,若是可以请您一并关注我的微博:http://weibo.com/xiaopenguu 每次更新都会在微博中同步更新
感谢您的关注。

0 0
原创粉丝点击