ios - 怎么把一个UIView的指定区域存成图片
迷茫
迷茫 2017-04-17 11:03:27
[iOS讨论组]

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

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

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

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(2)
黄舟
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];

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

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

不知道可以么,没有试。

阿神
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)];
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号