开发者社区> 问答> 正文

怎么把一个UIView的指定区域存成图片

我已经实现了把一个UIView直接生成图片。

UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
但是,我需要截取这个UIView的一部分,比如中间100个像素,怎么实现呢?

展开
收起
a123456678 2016-07-19 15:34:37 2117 0
2 条回答
写回答
取消 提交回答
  • - (UIImage *)getImageViewWithView:(UIView *)view {
        UIGraphicsBeginImageContext(view.frame.size);
        [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return image;
    }
    2019-07-17 19:58:26
    赞同 展开评论 打赏
  • [myView.layer renderInContext:UIGraphicsGetCurrentContext()];
    这个地方不是已经获取到当前图像的context了么,然后用路径裁剪获取需要的部分应该就可以吧。

    CGContextBeginPath(context);

    CGContextAddRect(context, rect)
    CGContextClip(context);
    2019-07-17 19:58:26
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载