2013-03-25 84 views
0

我目前使用UIGraphicsBeginImageContext(resultingImageSize);来创建图像。CGContext的可变大小

但是当我调用这个函数时,我不知道resultingImageSize的宽度。实际上,我开发了一些消耗大量内存的视频处理,并且我不能先处理然后再绘制:在期间我必须画出视频处理。

如果我设置,例如UIGraphicsBeginImageContext(CGSizeMake(300, 400));,超过400的绘制部分丢失。

那么有没有一种解决方案来设置一个可变大小的CGContext,或调整一个CGContext 几乎没有内存消耗

回答

0

我发现一个解决方案,每次必须调整大小时创建一个新的较大的上下文。这里的神奇功能:

void MPResizeContextWithNewSize(CGContextRef *c, CGSize s) { 
    size_t bitsPerComponents = CGBitmapContextGetBitsPerComponent(*c); 
    size_t numberOfComponents = CGBitmapContextGetBitsPerPixel(*c)/bitsPerComponents; 
    CGContextRef newContext = CGBitmapContextCreate(NULL, s.width, s.height, bitsPerComponents, sizeof(UInt8)*s.width*numberOfComponents, 
                CGBitmapContextGetColorSpace(*c), CGBitmapContextGetBitmapInfo(*c)); 
    // Copying context content 
    CGImageRef im = CGBitmapContextCreateImage(*c); 
    CGContextDrawImage(newContext, CGRectMake(0, 0, CGBitmapContextGetWidth(*c), CGBitmapContextGetHeight(*c)), im); 
    CGImageRelease(im); 

    CGContextRelease(*c); 
    *c = newContext; 
} 

我不知道是否可以优化,例如用memcpyas suggested here。我尝试过,但它使我的代码崩溃。