【问题标题】:Merging two images taking too much time合并两张图片需要太多时间
【发布时间】:2013-08-13 18:38:53
【问题描述】:

我正在开发我想合并图像的应用程序,但是当我迭代调用该函数时,它花费了太多时间。请告诉我如何改进函数的响应。

- (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次......所以我需要提高它的效率。

【问题讨论】:

  • 您是将 50 张图片合并为一张还是将 100 张图片合并为 50 张?
  • 是的...最后将 n 个图像合并为一个图像。
  • 那么你应该传入一个数组而不是每个数组的开始/结束上下文。那些上下文到图像的调用往往很昂贵。
  • imgimg2 有变化吗? cropRectwidth 是否更改?
  • 是的,每次调用函数时,每个参数都会发生变化。

标签: iphone ios ios5 ios4


【解决方案1】:

一些想法:

  1. 缓存结果(如果图像以相同的参数重复使用)
  2. 同时执行合并
  3. 重用上下文(使用CGBitmapContextCreate而不是UIGraphicsBeginImageContext创建上下文)
  4. 将插值质量设置为较小的值:CGContextSetInterpolationQuality(context, kCGInterpolationNone)

还有一些潜在的优化,但它们取决于图像的类型(alpha、大小、类型……)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    相关资源
    最近更新 更多