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

发布于 2022-08-24 08:57:44 字数 290 浏览 13 评论 0

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

UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

但是,我需要截取这个UIView的一部分,比如中间100个像素,怎么实现呢?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假扮的天使 2022-08-31 08:57:44
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];

这个地方不是已经获取到当前图像的context了么,然后用路径裁剪获取需要的部分应该就可以吧。

CGContextBeginPath(context);
    CGContextAddRect(context, rect)
    CGContextClip(context);

不知道可以么,没有试。

紫瑟鸿黎 2022-08-31 08:57:44
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, 1);
	[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
	UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
	
	//UIImageWriteToSavedPhotosAlbum(img, self, nil, nil);
	
	UIGraphicsEndImageContext();

self.view就是想要保存的view

我没看清楚题目 如果是保存指定区域的话,就把上述代码得到的image,通过下面的代码得到所需的指定区域图片并保存。

UIImage *resultImg = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(img.CGImage, rect)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文