我正在使用以下方法来模糊一些图像。 使用仪器CIImage正在泄漏。 我试着把它们包在@autoreleasepool里,但没有运气。 有什么想法吗?
-(UIImage *)blurImage:(UIImage *)image withStrength:(float)strength
{
@autoreleasepool {
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *inputImage = [[CIImage alloc] initWithCGImage:image.CGImage];
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
[filter setValue:inputImage forKey:@"inputImage"];
[filter setValue:[NSNumber numberWithFloat:strength] forKey:@"inputRadius"];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
float scale = [[UIScreen mainScreen] scale];
CIImage *cropped=[result imageByCroppingToRect:CGRectMake(0, 0, image.size.width*scale, image.size.height*scale)];
CGRect extent = [cropped extent];
CGImageRef cgImage = [context createCGImage:cropped fromRect:extent];
UIImage *returnImage = [UIImage imageWithCGImage:cgImage].copy;
CGImageRelease(cgImage);
return returnImage;
}
}
我看到您在分析代码时看到的相同泄漏。试试这个,这似乎可以避免泄漏并给你相同的结果:
- (UIImage*)blurImage:(UIImage*)image withStrength:(float)strength
{
@autoreleasepool {
CIImage* inputImage = [[CIImage alloc] initWithCGImage:image.CGImage];
CIFilter* filter = [CIFilter filterWithName:@"CIGaussianBlur"];
[filter setValue:inputImage forKey:@"inputImage"];
[filter setValue:[NSNumber numberWithFloat:strength] forKey:@"inputRadius"];
CIImage* result = [filter valueForKey:kCIOutputImageKey];
float scale = [[UIScreen mainScreen] scale];
CIImage* cropped = [result imageByCroppingToRect:CGRectMake(0, 0, image.size.width * scale, image.size.height * scale)];
return [[UIImage alloc] initWithCIImage:cropped];
}
}
你是否试图让 CIImages 归零?
-(UIImage *)blurImage:(UIImage *)image withStrength:(float)strength
{
//your code
CGImageRelease(cgImage);
cropped=nil;
result = nil;
inputImage = nil;
context = nil;
return returnImage;
}
}