比CGContextDrawImage更快地合并两个UIImages

时间:2012-05-29 15:28:38

标签: iphone objective-c ios ipad

我将两个UIImages合并到一个上下文中。它可以工作,但它执行得非常慢,我需要更快的解决方案。我的解决方案是在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);
}

有任何提高绩效的建议吗?

2 个答案:

答案 0 :(得分:0)

我肯定会建议使用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;
}

我不知道他们是否会更快或更快,但我真的不知道如何加速你拥有的东西,除非你的仪器配置文件发生故障。

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

答案 1 :(得分:0)

我没有设法找到合并图像的更快捷方式。我缩小了图像尺寸,使操作更快。