CGColorSpaceRelease多次调用引起的问题

问题描述:在多次运行图片Blur处理后,报如下错误:

Assertion failed: (!space->is_singleton), function color_space_dealloc, file ColorSpaces/color-space.c, line 100.

问题分析:Blur的代码如下:

- (UIImage *)blurryWithLevel:(CGFloat)level

{

if (level < 0.f || level > 1.f) {

level = 0.5f;

}

int boxSize = (int)(level * 100);

boxSize = boxSize - (boxSize % 2) + 1;

 

CGImageRef img = self.CGImage;

 

vImage_Buffer inBuffer, outBuffer;

vImage_Error error;

 

void *pixelBuffer;

 

CGDataProviderRef inProvider = CGImageGetDataProvider(img);

CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);

 

inBuffer.width = CGImageGetWidth(img);

inBuffer.height = CGImageGetHeight(img);

inBuffer.rowBytes = CGImageGetBytesPerRow(img);

 

inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);

 

pixelBuffer = malloc(CGImageGetBytesPerRow(img) *

CGImageGetHeight(img));

 

if(pixelBuffer == NULL)

NSLog(@"No pixelbuffer");

 

outBuffer.data = pixelBuffer;

outBuffer.width = CGImageGetWidth(img);

outBuffer.height = CGImageGetHeight(img);

outBuffer.rowBytes = CGImageGetBytesPerRow(img);

 

error = vImageBoxConvolve_ARGB8888(&inBuffer,

&outBuffer,

NULL,

0,

0,

boxSize,

boxSize,

NULL,

kvImageEdgeExtend);

 

 

if (error) {

NSLog(@"error from convolution %ld", error);

}

 

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef ctx = CGBitmapContextCreate(

outBuffer.data,

outBuffer.width,

outBuffer.height,

8,

outBuffer.rowBytes,

colorSpace,

(CGBitmapInfo)kCGImageAlphaNoneSkipLast);

CGImageRef imageRef = CGBitmapContextCreateImage (ctx);

UIImage *returnImage = [UIImage imageWithCGImage:imageRef];

 

CGContextRelease(ctx);

CGColorSpaceRelease(colorSpace);

 

free(pixelBuffer);

CFRelease(inBitmapData);

 

CGColorSpaceRelease(colorSpace);//此句多Release了
CGImageRelease(imageRef);

 

return returnImage;

}

问题解决:CGColorSpaceRelease多Release导致的。删除如上注释这句:CGColorSpaceRelease(colorSpace);