gpt4 book ai didi

ios - CIImage内存泄漏

转载 作者:可可西里 更新时间:2023-11-01 17:07:32 33 4
gpt4 key购买 nike

我正在使用以下方法模糊一些图像。使用仪器 CIImage 正在泄漏。我尝试将它们包装在 @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];
}
}

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

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