gpt4 book ai didi

ios - 如何将 CIFilter 应用于 UIImage?

转载 作者:可可西里 更新时间:2023-11-01 03:49:10 24 4
gpt4 key购买 nike

当我尝试将滤镜应用到用户选择的 UIImage 时,我的应用程序崩溃了(在不应用滤镜的情况下它一直运行良好)。我将“CoreImage”框架添加并导入到我的项目中,这样我就可以为用户选择的图像创建过滤器。

我试图通过为 UIImage 创建一个类别(基于 Apple's documentation ,然后在用户选择的 UIImage 上调用相应的方法来应用过滤器. 以下是我的类别标题和主体的代码;我做错了什么?(请注意,“randColor”是类别 UIColor 类方法,用于生成随机颜色)

#import <UIKit/UIKit.h>
#import <CoreImage/CoreImage.h>
#import "UIColor+CustomColorCategory.h"

@interface UIImage (MonoChromeFilter)

- (UIImage *) applyMonoChromeWithRandColor;

@end

#import "UIImage+MonoChromeFilter.h"

@implementation UIImage (MonoChromeFilter)

- (UIImage *)applyMonoChromeWithRandColor
{
CIContext *context = [CIContext contextWithOptions:nil];

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

CIFilter *filter = [CIFilter filterWithName:@"CIColorMonochrome"];

[filter setValue:ciImage forKey:kCIInputImageKey];
[filter setValue:[UIColor randColor] forKey:kCIAttributeTypeColor];

CIImage *result = [filter valueForKey:kCIOutputImageKey];

CGRect extent = [result extent];

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

UIImage *filteredImage = [[UIImage alloc] initWithCGImage:cgImage];

return filteredImage;
}

@end

这是 viewController 中调用该类别的方法:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^{
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
editedImage = [editedImage applyMonoChromeWithRandColor];

self.blogImageOutlet.image = editedImage;
self.blogImageOutlet.layer.cornerRadius = self.blogImageOutlet.frame.size.width / 2.0;
[self.blogImageOutlet setClipsToBounds:YES];

[self saveImageToLibrary:editedImage];

}];
}

最佳答案

我想通了!在调试并使用其他一些项目作为引用点之后,我意识到我遇到了两个问题。首先,我试图为 CIColor 使用 UIColor,这不可能直接实现。我首先必须将 UIColor 转换为 CIColor 才能应用它。接下来,我没有为 CIFilter 值键使用正确的字符串。以下是修改后的代码(现在可以使用了!)

#import "UIImage+MonoChromeFilter.h"

@implementation UIImage (MonoChromeFilter)

+ (UIImage *) applyMonoChromeWithRandColor: (UIImage *)uIImage
{

// Convert UIColor to CIColor
CGColorRef colorRef = [UIColor randColor].CGColor;
NSString *colorString = [CIColor colorWithCGColor:colorRef].stringRepresentation;
CIColor *coreColor = [CIColor colorWithString:colorString];

CIContext *context = [CIContext contextWithOptions:nil];

// Convert UIImage to CIImage
CIImage *ciImage = [[CIImage alloc] initWithImage:uIImage];

// Set values for CIColorMonochrome Filter
CIFilter *filter = [CIFilter filterWithName:@"CIColorMonochrome"];
[filter setValue:ciImage forKey:kCIInputImageKey];
[filter setValue:@1.0 forKey:@"inputIntensity"];
[filter setValue:coreColor forKey:@"inputColor"];

CIImage *result = [filter valueForKey:kCIOutputImageKey];

CGRect extent = [result extent];

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

UIImage *filteredImage = [[UIImage alloc] initWithCGImage:cgImage];

return filteredImage;
}

@end

关于ios - 如何将 CIFilter 应用于 UIImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23865101/

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