Iphone 展平子视图
我正在编写一个图形应用程序,它有一个UIView,随着时间的推移,它有几个UIImageView作为子视图添加到其中。Iphone 展平子视图,iphone,uiview,Iphone,Uiview,我正在编写一个图形应用程序,它有一个UIView,随着时间的推移,它有几个UIImageView作为子视图添加到其中。 由于性能原因,我想将所有这些子视图展平,因为它会随着时间的推移而变慢。“展平”这些层的最简单方法是什么 创建新的位图上下文: CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray(); CGContextRef newContext = CGBitmapContextCreate( NULL,
由于性能原因,我想将所有这些子视图展平,因为它会随着时间的推移而变慢。“展平”这些层的最简单方法是什么 创建新的位图上下文:
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
CGContextRef newContext =
CGBitmapContextCreate(
NULL,
viewContainingAllUIImageViews.frame.size.width,
vViewContainingAllUIImageViews.frame.size.height,
8,
viewContainingAllUIImageViews.frame.size.width,
colorspace,
0);
CGColorSpaceRelease(colorspace);
在上下文中绘制适当的背景:
CGContextSetRGBFillColor(newContext, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(newContext, CGRectMake(0, 0, viewContainingAllUIImageViews.frame.size.width, vViewContainingAllUIImageViews.frame.size.height));
获取UIImageView
包含的每个图像的CGImage
属性,并将所有图像绘制到此单个图像中:
CGContextDrawImage(newContext, oneOfTheSubImageViews.frame, oneOfTheSubImageViews.image.CGImage);
将位图上下文转换回图像:
CGImageRef newImage = CGBitmapContextCreateImage(newContext);
UIImage *flattenedImage = [UIImage imageWithCGImage:newImage];
然后
CFRelease
newContext
,newImage
,在UIImageView
中使用UIImage
,并放弃所有其他UIImageView
。我不清楚你所说的“扁平化”是什么意思。你能描述一下你遇到的性能限制以及为什么要“扁平化”吗这些观点能解决这些问题吗?展平是图形失真,还是将视图合并为一个视图?尼尔,我很确定他的意思是“将所有单独的图像合成为一个图像”(在Photoshop等处理图层时通常称为展平)。我的回答基于这个假设谢谢Matt。这很有帮助。我正在将其中的一部分集成到我的代码中。我发现了另外一个选项:只渲染整个父视图的层,而不是在每个子视图中循环:尽管如此,任何一种方法都可以很好地工作。