当前位置 : 主页 > 手机开发 > ios >

ios – CIImage内存泄漏

来源:互联网 收集:自由互联 发布时间:2021-06-11
我正在使用以下方法模糊一些图像.使用仪器CI Image正在泄漏.我尝试将它们包装在@autoreleasepool中,但没有运气.有任何想法吗? -(UIImage *)blurImage:(UIImage *)image withStrength:(float)strength{ @autore
我正在使用以下方法模糊一些图像.使用仪器CI Image正在泄漏.我尝试将它们包装在@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];
    }
}
网友评论