我正在从磁盘加载图像,将它们转换为CVPixelBufferRef,做一些事情,然后转换回UIImage以保存回磁盘。我试图确保我绝对释放所有必要的东西,但我的应用程序的内存使用量总是会增加,直到它崩溃。
for (//iterate through images) {
CVPixelBufferRef pixelBuffer = [self pixelBufferFromCGImage:image.CGImage];
// do some stuff
UIImage *newImage = [self imageFromPixelBuffer:pixelBuffer];
CVPixelBufferRelease(pixelBuffer);
}
以下是我使用的两种方法:
- (CVPixelBufferRef)pixelBufferFromCGImage:(CGImageRef)image {
CGSize frameSize = CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image));
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey, [NSNumber numberWithBool:YES],kCVPixelBufferCGBitmapContextCompatibilityKey, nil];
CVPixelBufferRef pxbuffer = NULL;
CVReturn status = CVPixelBufferCreate(
kCFAllocatorDefault, frameSize.width, frameSize.height,
kCVPixelFormatType_32BGRA, (__bridge CFDictionaryRef)options,
&pxbuffer);
NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);
CVPixelBufferLockBaseAddress(pxbuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(
pxdata, frameSize.width, frameSize.height,
8, CVPixelBufferGetBytesPerRow(pxbuffer),
rgbColorSpace,
(CGBitmapInfo)kCGBitmapByteOrder32Little |
kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
CGImageGetHeight(image)), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);
CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
CGImageRelease(image);
return pxbuffer;
}
- (UIImage *)imageFromPixelBuffer:(CVPixelBufferRef)pixelBuffer {
CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef myImage = [context createCGImage:ciImage fromRect:CGRectMake(0, 0, CVPixelBufferGetWidth(pixelBuffer), CVPixelBufferGetHeight(pixelBuffer))];
UIImage *image = [UIImage imageWithCGImage:myImage];
CGImageRelease(myImage);
return image;
}
在实际应用中,我当然不只是保存完全相同的图像,而是从第一个图像创建新的像素缓冲区,然后保存所有这些图像。但是,即使对于这个简单的示例,也存在内存增加问题。
我一定错过了一些基本的东西,这让我发疯。