2015-10-21 187 views
1
+ (CGImageRef) newFliteredCGImage:(CGImageRef)image withCIFilter:(CIFilter*)filter { 
    CIImage* input_image = [CIImage imageWithCGImage:image]; 
    NSSize image_size = NSMakeSize(CGImageGetWidth(image), 
            CGImageGetHeight(image)); 

    [filter setValue:input_image forKey:kCIInputImageKey]; 
    CIImage* output_image = [filter valueForKey:kCIOutputImageKey]; 

    CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef bitmap_context 
    = CGBitmapContextCreate(nil, 
          image_size.width, 
          image_size.height, 
          8, 
          image_size.width * 4, 
          color_space, 
          (CGBitmapInfo)kCGImageAlphaNoneSkipFirst); 
    CGColorSpaceRelease(color_space); 

    CIContext* ci_bitmap_context 
    = [CIContext contextWithCGContext:bitmap_context 
           options:nil]; 
    [ci_bitmap_context drawImage:output_image 
          inRect:[output_image extent] 
         fromRect:(CGRect){CGPointZero, image_size}]; 

    CGImageRef result_img = CGBitmapContextCreateImage(bitmap_context); 

    CGContextRelease(bitmap_context); 

    return result_img; 
} 

參數的CGImageRef和返回的CGImageRef由CGImageRelease釋放,但它有內存泄露CIContext內存泄露

我認爲CIContext有問題,但我不能找到問題

請給我幫忙

回答

1

做在@autoReleasePool ..

+ (CGImageRef) newFliteredCGImage:(CGImageRef)image withCIFilter:(CIFilter*)filter { 

@autoreleasepool { 
CIImage* input_image = [CIImage imageWithCGImage:image]; 
NSSize image_size = NSMakeSize(CGImageGetWidth(image), 
           CGImageGetHeight(image)); 

[filter setValue:input_image forKey:kCIInputImageKey]; 
CIImage* output_image = [filter valueForKey:kCIOutputImageKey]; 

CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB(); 
CGContextRef bitmap_context 
= CGBitmapContextCreate(nil, 
         image_size.width, 
         image_size.height, 
         8, 
         image_size.width * 4, 
         color_space, 
         (CGBitmapInfo)kCGImageAlphaNoneSkipFirst); 

CIContext* ci_bitmap_context 
= [CIContext contextWithCGContext:bitmap_context 
          options:nil]; 
[ci_bitmap_context drawImage:output_image 
         inRect:[output_image extent] 
        fromRect:(CGRect){CGPointZero, image_size}]; 

CGImageRef result_img = CGBitmapContextCreateImage(bitmap_context); 

return result_img; 

} 
} 

希望它^ h elps你..

+0

謝謝你的幫助! – WOF

+0

我的朋友很高興。 – Garry