【问题标题】:CIFilter how to turn white imageView.image with transparency to any colorCIFilter如何将具有透明度的白色imageView.image变成任何颜色
【发布时间】:2016-05-29 21:45:12
【问题描述】:

我正在尝试以编程方式将白色 imageView.image 变成不同的颜色。轮廓当前为白色。

我正在尝试将this solution 与 CIFilter 一起使用,但图像根本没有出现。

这是粘贴的代码供参考:

- (UIImage *) filterImage: (CIImage *)beginImage color: (UIColor *) color{

     CIImage *output = [CIFilter filterWithName:@"CIColorMonochrome" keysAndValues:kCIInputImageKey, beginImage, @"inputIntensity", [NSNumber numberWithFloat:1.0], @"inputColor", [[CIColor alloc] initWithColor:color], nil].outputImage;

     CIContext *context = [CIContext contextWithOptions:nil];
     CGImageRef cgiimage = [context createCGImage:output fromRect:output.extent];
     UIImage *newImage = [UIImage imageWithCGImage:cgiimage];

     CGImageRelease(cgiimage);

     return newImage;
}

【问题讨论】:

    标签: ios cifilter


    【解决方案1】:

    解决方案 .h 文件

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    
    @property (weak, readwrite, nonatomic) IBOutlet UIImageView *imgView;
    
    
    @end
    

    解决方案 .m 文件

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        CIImage *inputImage = [CIImage imageWithCGImage:[UIImage imageNamed:@"NJDLv"].CGImage];
    
    
    
        self.imgView.image = [self filterImage:inputImage color:[UIColor yellowColor]];
    }
    
    - (UIImage *) filterImage: (CIImage *)beginImage color: (UIColor *) color{
    
        CIColor *ciColor = [CIColor colorWithCGColor:color.CGColor];
    
        CIImage *output = [CIFilter filterWithName:@"CIColorMonochrome"
                                     keysAndValues:kCIInputImageKey, beginImage,
                           @"inputIntensity",@(1.0),
                           @"inputColor", ciColor, nil].outputImage;
    
        CIContext *context = [CIContext contextWithOptions:nil];
        CGImageRef cgiimage = [context createCGImage:output fromRect:output.extent];
        UIImage *newImage = [UIImage imageWithCGImage:cgiimage];
    
        CGImageRelease(cgiimage);
    
        return newImage;
    
    }
    
    @end
    

    【讨论】:

    • 谢谢,这仍然对我不起作用。图像保持白色。
    • 输入图像必须设置透明度。
    • 我已经使用答案中的方法添加了带有输入和输出的屏幕截图。
    • 如何设置透明度?
    • 你必须使用像gimp这样的图形软件。
    猜你喜欢
    • 2012-11-05
    • 1970-01-01
    • 2012-03-16
    • 2016-12-12
    • 1970-01-01
    • 2012-05-03
    • 2018-04-26
    • 2013-01-13
    • 2021-02-18
    相关资源
    最近更新 更多