比CGContextDrawImage更快地合并两个UIImages

Tho*_*sen 6 iphone objective-c ipad ios

我将两个UIImages合并到一个上下文中.它可以工作,但它执行得非常慢,我需要更快的解决方案.我的解决方案是mergeImage: withImage:在iPad 1G上拨打电话需要大约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;
}
Run Code Online (Sandbox Code Playgroud)

这是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);
}
Run Code Online (Sandbox Code Playgroud)

有什么建议可以提高性能吗?

Tho*_*sen 0

我没有找到一种更快的方法来合并图像。我缩小了图像尺寸以使操作更快。