Ios 将UIView输出到带有其他视图的UIImage的最佳方法”;插入“;在上面
我正在创建一个应用程序,允许用户在图像上放置“贴纸”。然后,我希望能够输出图像(以其原始分辨率),并在适当的位置附加标签 我正在存储与图像大小相关的所有位置和大小,所以这不是问题。问题是我不确定如何最好地输出图像 我可以创建一个Ios 将UIView输出到带有其他视图的UIImage的最佳方法”;插入“;在上面,ios,swift,uiimage,Ios,Swift,Uiimage,我正在创建一个应用程序,允许用户在图像上放置“贴纸”。然后,我希望能够输出图像(以其原始分辨率),并在适当的位置附加标签 我正在存储与图像大小相关的所有位置和大小,所以这不是问题。问题是我不确定如何最好地输出图像 我可以创建一个UIView,其中包含图像和子视图等。。。然后将该视图渲染为UIImage。但对于大型图像,我有一种感觉,要么速度慢,要么内存不足 这是唯一的办法吗?我觉得应该有一些CIImage合成的东西可以做我想要的,但不确定是否有一个UIImage和UIViews几年前我写了一些类
UIView
,其中包含图像和子视图等。。。然后将该视图渲染为UIImage。但对于大型图像,我有一种感觉,要么速度慢,要么内存不足
这是唯一的办法吗?我觉得应该有一些CIImage合成的东西可以做我想要的,但不确定是否有一个
UIImage
和UIViews
几年前我写了一些类似于UIImage的东西
// NOTE! this method should only be called from the main thread because of
// UIGraphicsGetImageFromCurrentImageContext();
- (UIImage *)merge:(UIImage *)image atRect:(CGRect)pos overlay:(BOOL)overlay fillColor:(UIColor *)fill
{
UIGraphicsBeginImageContext(self.size);
UIImage *bottom = (overlay)?self:image;
UIImage *top = (overlay)?image:self;
CGRect lf = CGRectMake(0, 0, self.size.width, self.size.height);
CGRect bottomRect = (overlay)?lf:pos;
CGRect topRect = (overlay)?pos:lf;
if (fill){
[fill setFill];
CGContextFillRect (UIGraphicsGetCurrentContext(), topRect);
}
[bottom drawInRect:bottomRect];
[top drawInRect:topRect];
UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return destImage;
}
这是我做这件事的一种方式(但不明确确定解决方案)
`
希望这能解决你的问题
extension UIView {
var snapshot: UIImage? {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0)
self.drawHierarchy(in: self.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
`