gpt4 book ai didi

iphone - UIGraphicsContext 内存泄漏

转载 作者:行者123 更新时间:2023-11-29 13:03:50 24 4
gpt4 key购买 nike

您好 在我的应用程序中,我有一个函数可以获取当前 View 的图像并将其转换为模糊图像,然后将其添加到 current.view。尽管我使用 [remove from superview] 删除了 View ,但内存仍然很高。我正在使用核心图形并将所有 UI 图像设置为零。

我收到内存泄漏警告 enter image description here

-(void)burImage
{
//Get a screen capture from the current view.
UIGraphicsBeginImageContext(CGSizeMake(320, 450));
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//Blur the image
CIImage *blurImg = [CIImage imageWithCGImage:viewImg.CGImage];

CGAffineTransform transform = CGAffineTransformIdentity;
CIFilter *clampFilter = [CIFilter filterWithName:@"CIAffineClamp"];
[clampFilter setValue:blurImg forKey:@"inputImage"];
[clampFilter setValue:[NSValue valueWithBytes:&transform objCType:@encode(CGAffineTransform)] forKey:@"inputTransform"];

CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
[gaussianBlurFilter setValue:clampFilter.outputImage forKey: @"inputImage"];
[gaussianBlurFilter setValue:[NSNumber numberWithFloat:22.0f] forKey:@"inputRadius"];

CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImg = [context createCGImage:gaussianBlurFilter.outputImage fromRect:[blurImg extent]];
UIImage *outputImg = [UIImage imageWithCGImage:cgImg];

//Add UIImageView to current view.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 450)];
[imgView setTag:1109];

imgView.image = outputImg;
[imgView setTag:1108];

gaussianBlurFilter = nil;
outputImg = nil;
blurImg = nil;

viewImg = nil;
[self.view addSubview:imgView];
UIGraphicsEndImageContext();
}

最佳答案

静态分析器(Xcode“产品”菜单上的“分析”)通知您在方法末尾缺少所需的 CGImageRelease(cgImg)。如果您有从名称中带有“Create”或“Copy”的方法/函数返回的核心基础对象,则您有责任释放它。

顺便说一下,如果你点击 enter image description here图标(一次出现在页边空白处,一次出现在错误消息中出现的版本上),它将向您显示更多信息:

enter image description here

这有助于追溯问题的根源,在本例中是对 createCGImage 的调用。如果您查看 documentation对于 createCGImage,它确认了这个诊断,报告:

Return Value

A Quartz 2D image. You are responsible for releasing the returned image when you no longer need it.

有关发布 Core Foundation 对象的总顾问,请参阅 Create RuleCore Foundation 的内存管理编程指南中。

关于iphone - UIGraphicsContext 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19124503/

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