objective-c - 应用核心图像过滤器 - 应用程序崩溃

标签 objective-c ios crash core-image

我使用以下代码来应用几种类型的图像过滤器。 (还有三个用于亮度、饱和度和对比度的“editImage”函数,具有通用的completeImageUsingOutput 方法)。我使用 slider 来改变它们的值。 如果我单独与他们中的任何一个合作,效果都很好。一旦我对两个不同的过滤器进行两个函数调用,应用程序就会崩溃。

编辑: didReceiveMemoryWarning 被调用。我使用内存泄漏工具查看内存分配,每次编辑后内存分配增加约 15mb

崩溃发生在
CGImageRef cgimg = [context createCGImage:outputImage fromRect:outputImage.extent];

此外,如果将completeImageUsingOutputImage方法的指令放入各个函数中,我就可以使用两种类型的过滤器而不会崩溃。一旦我调用第三个,应用程序就会崩溃。

(过滤器和上下文已声明为实例变量并在 init 方法中初始化)

- (UIImage *)editImage:(UIImage *)imageToBeEdited tintValue:(float)tint
{
CIImage *image = [[CIImage alloc] initWithImage:imageToBeEdited];
NSLog(@"in edit Image:\ncheck image: %@\ncheck value:%f", image, tint);

[tintFilter setValue:image forKey:kCIInputImageKey];
[tintFilter setValue:[NSNumber numberWithFloat:tint] forKey:@"inputAngle"];

CIImage *outputImage = [tintFilter outputImage];
NSLog(@"check output image: %@", outputImage);
return [self completeEditingUsingOutputImage:outputImage];
}

- (UIImage *)completeEditingUsingOutputImage:(CIImage *)outputImage
{
CGImageRef cgimg = [context createCGImage:outputImage fromRect:outputImage.extent];
NSLog(@"check cgimg: %@", cgimg);
UIImage *newImage = [UIImage imageWithCGImage:cgimg];
NSLog(@"check newImge: %@", newImage);
CGImageRelease(cgimg);
return newImage;
}

编辑:在缩小尺寸的图像上使用这些滤镜现在可以正常工作,但如果我知道为什么之前没有释放一些内存,那就太好了。

最佳答案

将此行添加到completeEditingUsingOutputImage:方法的最顶部

    CIContext *context = [CIContext contextWithOptions:nil];

这也是获取 CIImage 的方法:

    CIImage *outputImage = [tintFilter valueForKey:@"outputImage"];

关于objective-c - 应用核心图像过滤器 - 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12262618/

相关文章:

iphone - double 中的 objective-c stringvalue

ios - 使用 ReactiveCocoa 将 UISwitch 的状态绑定(bind)到模型

ios - 有没有办法在屏幕上显示一条短消息并让它消失?

c++ - Linux:针对经常崩溃的 C++ 和 FORTRAN 程序提供系统保护

iphone - 删除标签的内容 - Objective C

ios - 如何在 Swift/Objective C 中从 HTTP 请求返回数据

iphone - Objective C : "_main", 引用自 : Start in crt1. 3.1.o 错误

objective-c - 加载纹理时动画屏幕

python - 每次我在终端中运行python命令时,系统崩溃,如何摆脱/opt/miniconda3/bin/python?

ios - AFNetworking:如何在请求中设置Content-Type HTTP header ?