我正在使用以下方法模糊一些图像.使用仪器CI Image正在泄漏.我尝试将它们包装在@autoreleasepool中,但没有运气.有任何想法吗? -(UIImage *)blurImage:(UIImage *)image withStrength:(float)strength{ @autore
-(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]; } }