CIFilter如何将具有透明度的白色imageView.image转换为任何颜色

时间:2016-02-17 22:53:04

标签: ios cifilter

我正在尝试以编程方式将白色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;
}

enter image description here

1 个答案:

答案 0 :(得分:0)

解决方案.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

enter image description here