2015-11-07 103 views
0

我收到內存警告3次,然後應用程序崩潰。收到內存警告目標c

我有一個滑塊,需要將模糊值更改爲UIImageView圖像。 我嘗試使用dispatch_async並使用imageView.image = nil清理圖像內存;但問題沒有解決。

從uislider此代碼調用:

dispatch_async(dispatch_get_main_queue(), ^(void) 
     { 
     imageView.image =nil; 
     imageView.image = [self blurImage:originalImage withValue:value]; 
     }); 




-(UIImage *)blurImage:(UIImage *)theImage withValue:(nullable id)value 
{ 
    CIFilter *gaussianBlurFilter = [CIFilter filterWithName:@"CIGaussianBlur"]; 
    [gaussianBlurFilter setDefaults]; 
    CIImage *inputImage = [CIImage imageWithCGImage:[theImage CGImage]]; 
    [gaussianBlurFilter setValue:inputImage forKey:kCIInputImageKey]; 
    [gaussianBlurFilter setValue:value forKey:kCIInputRadiusKey]; 

    CIImage *outputImage = [gaussianBlurFilter outputImage]; 
    CIContext *context = [CIContext contextWithOptions:nil]; 
    CGImageRef cgimg  = [context createCGImage:outputImage fromRect:[inputImage extent]]; 
    UIImage *image  = [UIImage imageWithCGImage:cgimg]; 

    return image; 
} 

回答

1

其中一個假設是你沒有釋放CIImageRef cgimg。這可能會增加內存。

-(UIImage *)blurImage:(UIImage *)theImage withValue:(nullable id)value 
{ 
    CIFilter *gaussianBlurFilter = [CIFilter filterWithName:@"CIGaussianBlur"]; 
    [gaussianBlurFilter setDefaults]; 
    CIImage *inputImage = [CIImage imageWithCGImage:[theImage CGImage]]; 
    [gaussianBlurFilter setValue:inputImage forKey:kCIInputImageKey]; 
    [gaussianBlurFilter setValue:value forKey:kCIInputRadiusKey]; 

    CIImage *outputImage = [gaussianBlurFilter outputImage]; 
    CIContext *context = [CIContext contextWithOptions:nil]; 
    CGImageRef cgimg  = [context createCGImage:outputImage fromRect:[inputImage extent]]; 
    UIImage *image  = [UIImage imageWithCGImage:cgimg]; 
    //Release the core image reference 
    CGImageRelease(cgimg) 
    return image; 
} 
+0

謝謝它的作品( - : –