繁体   English   中英

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

[英]CIFilter how to turn white imageView.image with transparency to any color

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

我正在尝试将此解决方案与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;
}

在此输入图像描述

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

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM