2012-02-06 50 views
0

我在两个步骤中使用CGContext:首先创建上下文,绘制背景图像,用UIBezierPaths绘制,然后获取图像&释放上下文。其次这个图像与另一个像这样的组合:CGContext在iOS5中缓慢

UIGraphicsBeginImageContextWithOptions(self.anchorImage.size, NO, 1); 
[self.anchorImage drawInRect:CGRectMake(0, 0, self.anchorImage.size.width, self.anchorImage.size.height)]; 
[tempImage drawInRect:CGRectMake(0, 0, self.anchorImage.size.width, self.anchorImage.size.height)]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

这iOS4的效果不错,但它是非常在iOS5中(我测试在3GS)非常缓慢。难道我做错了什么?或者是否有更好的方式来做到这一点?或者有没有特定的iOS5方法?

+0

是否有理由关闭第一个上下文,而不是仅仅在第一个上下文中的UIBezierPaths上绘制第二个图像? – 2012-02-06 14:35:12

回答

2

如果您只是生成图像或绘制到视图中,我不清楚。无论如何,为了提高速度,您可以将这些图像的生成转移到不同的调度队列,所以您当前的线程(可能是主线程?)不会被阻塞。我想用CGBitmapContextCreate结合CGBitmapContextCreateImage。当生成最终图像时,请更新图像视图(或者对图像执行任何其他操作)。

dispatch_async(your_async_queue, ^() { 
    CGContextRef ctx = CGBitmapContextCreate(/* params */); 

    // your drawing 

    CGImageRef imageRef = CGBitmapContextCreateImage(context); 
    UIImage * image = [[UIImage alloc] initWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 
    CGContextRelease(context); 

    dispatch_async(dispatch_get_main_queue(), ^() { 
     // do something with image on the main thread 
    }); 
} 

查看完整方法签名的CGBitmapContext documentation