CIImage 内存泄漏

编程入门 行业动态 更新时间:2024-10-26 12:34:26
本文介绍了CIImage 内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下方法来模糊一些图像.使用仪器 CIImage 正在泄漏.我尝试将它们包装在 @autoreleasepool 中,但没有运气.有什么想法吗?

I'm using the below method to blur some images. Using instruments the CIImage's are leaking. I tried wrapping them in an @autoreleasepool, but no luck. Any ideas?

-(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;
    }

}

推荐答案

我看到了与您在分析代码时看到的相同的泄漏.试试这个,这似乎可以避免泄漏并为您提供相同的结果:

I see the same leak you're seeing when profiling the code. Try this instead which seems to avoid the leak and give you the same results:

- (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];
    }
}

这篇关于CIImage 内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-29 00:07:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/744733.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内存   CIImage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!