我正在开发我想合并图像的应用程序,但是当我迭代调用该函数时,它花费了太多时间。任何人请告诉我如何改进该函数的响应。
- (UIImage *) addImageToImage:(UIImage *)img withImage2:(UIImage *)img2 andRect:(CGRect)cropRect withImageWidth:(int) width
{
CGSize size = CGSizeMake(width,40);
UIGraphicsBeginImageContext(size);
CGPoint pointImg1 = CGPointMake(0,0);
[img drawAtPoint:pointImg1];
CGPoint pointImg2 = cropRect.origin;
[img2 drawAtPoint: pointImg2];
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
这个函数在迭代中被调用了 30 到 50 次...所以我需要提高它的效率。
最佳答案
一些想法:
- 缓存结果(如果使用相同参数重复使用图像)
- 同时执行合并
- 重用上下文(使用
CGBitmapContextCreate
而不是UIGraphicsBeginImageContext
创建上下文) - 将插值质量设置为较小的值:
CGContextSetInterpolationQuality(context, kCGInterpolationNone)
还有一些潜在的优化,但它们取决于图像的类型(alpha、大小、类型……)。
关于iphone - 合并两个图像花费太多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18187201/