首页 > 移动平台 > 详细

IOS开发:截取当前视图

时间:2016-05-13 03:27:47      阅读:304      评论:0      收藏:0      [点我收藏+]

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

+(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 每次更新都会在微博中同步更新
感谢您的关注。

IOS开发:截取当前视图

原文:http://blog.csdn.net/xp1819/article/details/51337423

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!