gpt4 book ai didi

ios - CGImageRef 消耗大量内存

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:54 26 4
gpt4 key购买 nike

我正在为我的一个应用程序屏幕创建模糊图像,为此我使用以下代码


UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CIContext *context = [CIContext contextWithOptions:nil];
CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage];


CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
[filter setValue:inputImage forKey:kCIInputImageKey];
[filter setValue:[NSNumber numberWithFloat:5] forKey:@"inputRadius"];
CIImage *result = [filter valueForKey:kCIOutputImageKey];

CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];

blurrImage = [UIImage imageWithCGImage:cgImage];
self.blurrImageView.image = blurrImage;
CGImageRelease(cgImage);


根据上面的代码,我得到了正确的模糊图像,但问题出在 CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]]; 这一行。

到这行显示内存占用正常,但是这行之后内存占用增加异常高,

听到的是执行前显示的内存使用情况的屏幕截图。内存使用量随着这个方法的执行而不断增加,这是之前

enter image description here

这是在执行行 CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];

之后

enter image description here

这是常见行为吗..?我搜索了答案,但我没有得到,所以任何人遇到同样的问题请帮助我

有一件事我“没有使用 ARC”

最佳答案

我在使用 Core Image 时遇到了同样的内存消耗问题。

如果您正在寻找替代品,在 iOS 7 中,您可以使用 UIImage+ImageEffects 类别,它作为 iOS_UIImageEffects 项目的一部分在 WWDC 2013 sample code 页面提供.它提供了一些新方法:

- (UIImage *)applyLightEffect;
- (UIImage *)applyExtraLightEffect;
- (UIImage *)applyDarkEffect;
- (UIImage *)applyTintEffectWithColor:(UIColor *)tintColor;
- (UIImage *)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor *)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage *)maskImage;

这些不会遇到您在使用 Core Image 时遇到的内存消耗问题。 (另外,它是一种更快的模糊算法。)

此技术在 WWDC 2013 视频 Implementing Engaging UI on iOS 中有说明。

关于ios - CGImageRef 消耗大量内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20627147/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com