iOS - 无法使用 CIFilter 处理图像

发布于 2024-12-21 09:10:32 字数 1080 浏览 2 评论 0原文

我正在尝试使用 Core Image 处理图像。我创建了 UIImage 类别来做到这一点。

我已将 QuartzCoreCoreImage 框架添加到项目中,导入 CoreImage/CoreImage.h 并使用此代码:

CIImage *inputImage = self.CIImage;
CIFilter *exposureAdjustmentFilter = [CIFilter filterWithName:@"CIExposureAdjust"];
[exposureAdjustmentFilter setDefaults];
[exposureAdjustmentFilter setValue:inputImage forKey:@"inputImage"];
[exposureAdjustmentFilter setValue:[NSNumber numberWithFloat:5.0f] forKey:@"inputEV"];
CIImage *outputImage = [exposureAdjustmentFilter valueForKey:@"outputImage"];
CIContext *myContext = [CIContext contextWithOptions:nil];

return [UIImage imageWithCGImage:[myContext createCGImage:outputImage fromRect:outputImage.extent]];

但我有 nil 过滤器的输出图像。

我也尝试使用 CIHueAdjust 得到相同的结果。

比你提前

更新:我已经找到了解决方案。有必要分配新的CIImage,而不仅仅是通过这种方式传递对UIImage.CIImage的引用:

CIImage *inputImage = [[CIImage alloc] initWithImage:self];

I am trying to process image using Core Image. I have created UIImage category to do it.

I have added QuartzCore and CoreImage frameworks to project, imported CoreImage/CoreImage.h and used this code:

CIImage *inputImage = self.CIImage;
CIFilter *exposureAdjustmentFilter = [CIFilter filterWithName:@"CIExposureAdjust"];
[exposureAdjustmentFilter setDefaults];
[exposureAdjustmentFilter setValue:inputImage forKey:@"inputImage"];
[exposureAdjustmentFilter setValue:[NSNumber numberWithFloat:5.0f] forKey:@"inputEV"];
CIImage *outputImage = [exposureAdjustmentFilter valueForKey:@"outputImage"];
CIContext *myContext = [CIContext contextWithOptions:nil];

return [UIImage imageWithCGImage:[myContext createCGImage:outputImage fromRect:outputImage.extent]];

But I have got nil output image from the filter.

I have also tried to use CIHueAdjust with the same result.

Than you in advance

UPDATE: I have found solution. It was necessary to alloc new CIImage, not only pass reference to UIImage.CIImage this way:

CIImage *inputImage = [[CIImage alloc] initWithImage:self];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲鸠 2024-12-28 09:10:32

尝试以下代码:-

 CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"old-country-rain.jpg"]];
CIFilter * controlsFilter = [CIFilter filterWithName:@"CIExposureAdjust"];
[controlsFilter setValue:inputImage forKey:kCIInputImageKey];
[controlsFilter setValue:[NSNumber numberWithFloat: 2.0f] forKey:@"inputEV"];
NSLog(@"%@",controlsFilter.attributes);
CIImage *displayImage = controlsFilter.outputImage;
UIImage *finalImage = [UIImage imageWithCIImage:displayImage];

CIContext *context = [CIContext contextWithOptions:nil];
if (displayImage == nil || finalImage == nil) {
    // We did not get output image. Let's display the original image itself.
    imageView.image = [UIImage imageNamed:@"old-country-rain.jpg"];
}else {
    // We got output image. Display it.
    imageView.image = [UIImage imageWithCGImage:[context createCGImage:displayImage fromRect:displayImage.extent]];
}

Try following code:-

 CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"old-country-rain.jpg"]];
CIFilter * controlsFilter = [CIFilter filterWithName:@"CIExposureAdjust"];
[controlsFilter setValue:inputImage forKey:kCIInputImageKey];
[controlsFilter setValue:[NSNumber numberWithFloat: 2.0f] forKey:@"inputEV"];
NSLog(@"%@",controlsFilter.attributes);
CIImage *displayImage = controlsFilter.outputImage;
UIImage *finalImage = [UIImage imageWithCIImage:displayImage];

CIContext *context = [CIContext contextWithOptions:nil];
if (displayImage == nil || finalImage == nil) {
    // We did not get output image. Let's display the original image itself.
    imageView.image = [UIImage imageNamed:@"old-country-rain.jpg"];
}else {
    // We got output image. Display it.
    imageView.image = [UIImage imageWithCGImage:[context createCGImage:displayImage fromRect:displayImage.extent]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文