ios - 如何提高 drawRect 性能以将不同的 UIImages 合并在一起?

标签 ios performance drawrect

我需要改进我的“drawRect”方法。它非常慢。 我有 10 个透明的 png,我想用不同的混合模式合并在一起。 我读过来自 Apple Developer Lib 的帖子来自Stackoverflow .

现在我想知道如何改进我的代码和整体绘图性能。 请你帮助我好吗?在一个 for 循环中加载所有这些图像会很慢,对吧? 我如何获得最佳性能?

  - (void)drawRect:(CGRect)rect {

        CGContextRef context = UIGraphicsGetCurrentContext();
        CGAffineTransform matrix =CGContextGetCTM(context);
        CGAffineTransformInvert(matrix);
        CGContextConcatCTM(context,matrix);

            CGRect contentRect;

             // load each UIImage
        for (i=0; i< [configurationArray count]; i = i + 1) {   
                    image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:nil]];

    contentRect = CGRectMake(x,y,image.size.width,image.size.height);
    CGContextSetBlendMode (context, kCGBlendModeScreen);
    CGContextSetAlpha (context, [layerData.layerAlpha floatValue]);


    CGContextDrawImage (context,contentRect, image.CGImage);

    }
}

更新:

现在我使用 CALayers 将像素直接放在需要的位置。 但我需要不同的混合模式、alpha 和每一层的颜色填充。 有没有办法避免在每一层上调用昂贵的“CGContextDrawImage”方法? 例如,我希望可以只更新特定层的小尺寸的上下文,对吗? 或者我是否需要为每个上下文更改呈现整个 UIView?

我希望有人能把我推向正确的方向。 我只需要学习那个话题。 感谢您的帮助。

- (void)drawRect:(CGRect)rect {
 // load each UIImage
        for (int i=0; i< [myArray count]; i++) { 

        image = [[ImageCache sharedImageCache] imageForKey:imageName];

       CALayer* sublayer = [CALayer layer] ;

        [sublayer setPosition:CGPointMake(image.size.width/2, image.size.height/2)]; 
        [sublayer setFrame:CGRectMake(x, y, image.size.width, image.size.height)];

        CGImageRef layerImage = [image CGImage];
        [sublayer setContents:(__bridge id) layerImage];
        [sublayer setBackgroundColor:[[UIColor clearColor] CGColor]];
        [self.layer addSublayer:sublayer];

// How can i set the blend mode, color fill & alpha value without 
// calling the expensive "CGContextDrawImage" ? Possible?

}
}

最佳答案

根据 Brad 的评论:

You probably shouldn't load your ten images within -drawRect:. Load and cache them somewhere else first. Be careful about memory usage with that many images, though.

我以 Brad 的方法结束。

关于ios - 如何提高 drawRect 性能以将不同的 UIImages 合并在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10032010/

相关文章:

sql - 当我想消除重复记录时,Union all 和 union 应该使用哪一个?

Java:通过将应用程序升级为 Web 服务来测量应用程序的性能

sql - 多个 INSERT 语句与具有多个 VALUES 的单个 INSERT

iOS CGPath 性能

ios - 使用 JSQMessage 下载后难以让媒体替换占位符

ios - NSFileManager -createFileAtPath 因 NSInvalidArgumentException 而失败

ios - 将 UIImage 从 View Controller 传递到另一个

objective-c - 在UITableView中显示字典数组

ios - 是否可以在没有继承的情况下覆盖类上的 drawRect?

ios - iOS 中的绘制矩形