iphone - 合并两个 UIImages 比 CGContextDrawImage 更快

标签 iphone objective-c ios ipad

我正在将两个 UIImage 合并到一个上下文中。它可以工作,但执行起来很慢,我需要一个更快的解决方案。因为我的解决方案是在 iPad 1G 上调用 mergeImage: withImage: 大约需要 400 毫秒。

这是我的做法:

-(CGContextRef)mergeImage:(UIImage*)img1 withImage:(UIImage*)img2
{
    CGSize size = [ImageToolbox getScreenSize];
    CGContextRef context = [ImageToolbox createARGBBitmapContextFromImageSize:CGSizeMake(size.width, size.height)];

    CGContextSetRenderingIntent(context, kCGRenderingIntentSaturation);

    CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), img1.CGImage);
    CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), img2.CGImage);


    return context;
}

下面是 ImageToolbox 类的静态方法:

static CGRect screenRect;

+ (CGContextRef)createARGBBitmapContextFromImageSize:(CGSize)imageSize
{
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;

    size_t pixelsWide = imageSize.width;
    size_t pixelsHigh = imageSize.height;

    bitmapBytesPerRow   = (pixelsWide * 4);
    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

    colorSpace = CGColorSpaceCreateDeviceRGB();
    if (colorSpace == NULL)
    {
        fprintf(stderr, "Error allocating color space\n");
        return NULL;
    }

    bitmapData = malloc( bitmapByteCount );
    if (bitmapData == NULL)
    {
        fprintf (stderr, "Memory not allocated!");
        CGColorSpaceRelease( colorSpace );
        return NULL;
    }

    context = CGBitmapContextCreate (bitmapData,
                                     pixelsWide,
                                     pixelsHigh,
                                     8,      // bits per component
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedFirst);
    if (context == NULL)
    {
        free (bitmapData);
        fprintf (stderr, "Context not created!");
    }

    CGColorSpaceRelease( colorSpace );

    return context;
}

+(CGSize)getScreenSize
{
    if (screenRect.size.width == 0 && screenRect.size.height == 0)
    {
        screenRect = [[UIScreen mainScreen] bounds];    

    }
    return CGSizeMake(screenRect.size.height, screenRect.size.width-20);
}

有什么提高性能的建议吗?

最佳答案

我肯定会推荐使用 Instruments 来分析哪些消息花费的时间最多,这样您就可以真正地分解它。此外,我已经编写了一些方法,我认为它们应该用更少的代码来做同样的事情,但是你必须按照你所做的方式写出所有内容才能真正保持可定制性。不管怎样,它们在这里:

-(CGContextRef)mergeImage:(UIImage *)img1 withImage:(UIImage *)img2
{  
  CGSize size = [ImageToolbox getScreenSize];
  CGRect rect = CGRectMake(0, 0, size.width, size.height);

  UIGraphicsBeginImageContextWithOptions(size, YES, 1.0);

  CGContextRef context = UIGraphicsGetCurrentContext();

  CGContextSetRenderingIntent(context, kCGRenderingIntentSaturation);

  [img1 drawInRect:rect];
  [img2 drawInRect:rect];

  UIGraphicsEndImageContext();

  return context;
}

或者如果您想要立即合成图像:

- (UIImage *)mergeImage:(UIImage *)img1 withImage:(UIImage *)img2
{
  CGSize size = [ImageToolbox getScreenSize];
  CGRect rect = CGRectMake(0, 0, size.width, size.height);

  UIGraphicsBeginImageContextWithOptions(size, YES, 1.0);

  CGContextRef context = UIGraphicsGetCurrentContext();

  CGContextSetRenderingIntent(context, kCGRenderingIntentSaturation);

  [img1 drawInRect:rect];
  [img2 drawInRect:rect];

  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

  return image;
}

我不知道它们是否会更快,但我真的不知道如何轻松地加快您所拥有的速度,除非您将仪器配置文件分解。

无论如何,我希望这对您有所帮助。

关于iphone - 合并两个 UIImages 比 CGContextDrawImage 更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10802189/

相关文章:

ios - 使用自定义单元格滚动时 uitableview 上的重复行

iPhone 邮件应用程序

iphone - 是否可以定义适应可变数据的日期格式?

ios - 安装 ios-buildkit 时出错

html - 在没有互联网的情况下在 iPad 上下载和查看原型(prototype)

ios - xcode iOS 增加UIButton点击区域

css - 移动优化 : Site wrapper fills screen at 1000px even though iPhone 5s screen is 640px?

iphone - 如何添加 splitViewController

iphone - Facebook API 名称中缺少感叹号

ios - 需要 xcdatamodel 吗?