展平子视图

时间:2009-10-29 03:03:31

标签: iphone uiview

我有一个我正在编写的图形应用程序,它有一个UIView,随着时间的推移添加了多个UIImageViews。 出于性能原因,我希望将所有这些子视图展平,因为它随着时间的推移而变慢。 “压扁”这些图层的最简单方法是什么?

1 个答案:

答案 0 :(得分:2)

创建一个新的位图上下文:

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));

获取CGImage包含的每张图片的UIImageView属性,并将所有图片绘制到此单张图片中:

CGContextDrawImage(newContext, oneOfTheSubImageViews.frame, oneOfTheSubImageViews.image.CGImage);

将位图上下文转换回图像:

CGImageRef newImage = CGBitmapContextCreateImage(newContext);
UIImage *flattenedImage = [UIImage imageWithCGImage:newImage];

然后CFRelease newContextnewImage,使用UIImage中的UIImageView并弃掉所有其他UIImageView

相关问题