Объединение двух UIImage быстрее, чем CGContextDrawImage

Я объединяю два UIImages в один контекст. Он работает, но работает довольно медленно, и мне нужно более быстрое решение. В моем решении требуется около 400 мс, чтобы выполнить вызов mergeImage: withImage:на iPad 1G.

Вот что я делаю:

-(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);
}

Есть предложения по повышению производительности?

6
задан Thomas Clemensen 29 May 2012 в 15:28
поделиться