1.创建图形上下文
2.将view的layer渲染到图形上下文
3.从图形上下文得到图片
4.关闭图像上下文
#pragma mark - (方法)选择UIImageView所add的view上-即可绘制为包含view的新图片
- (void)cutView:(UIView *)view success:(void(^)(UIImage *image))success {
//开启图形上下文
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0);
//获取当前上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//渲染
[view.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
success(newImage);
}
#prama mark -(调用)
[self cutView:此处是你要保存的view success:^(UIImage *image) {
//处理绘制的新image
}];
本文介绍了一种在iOS中将任意view转换为图片的方法。通过创建图形上下文,将目标view渲染到该上下文中,随后从上下文中获取图片并关闭上下文,最终返回新生成的图片。这种方法适用于需要将屏幕特定部分保存为图片的场景。
1万+

被折叠的 条评论
为什么被折叠?



