API & Property

/// 初始化
- (instancetype)kj_initWithOriImage:(UIImage *)image ExtendParameterBlock:(void(^_Nullable)(KJRenderImageView *obj))paramblock;
/// 修改作用
- (void)kj_changeRenderInfo:(KJRenderInfo *)info;
@interface KJRenderInfo : NSObject
@property(nonatomic,assign)KJRenderFilterType type; /// 滤镜
@property(nonatomic,assign)float opacity;/// 透明度,默许 1
@property(nonatomic,assign)float extend;/// 扩展
@property(nonatomic,strong)UIColor *color;/// 色彩滤镜
@property(nonatomic,assign)KJRenderCoreImageFilterType coreType;/// CoreImage 滤镜
@end
滤镜是否会蒙蔽你的双眼?

简略介绍思路

KJRenderInfo滤镜模型数据

现在有四种滤镜

typedef NS_OPTIONS(NSInteger, KJRenderFilterType) {
    KJRenderFilterTypeOriginal,  /// 原片
    KJRenderFilterTypeColor,     /// 色彩滤镜
    KJRenderFilterTypeSketch,    /// 素描滤镜
    KJRenderFilterTypeCoreImage, /// CoreImage 自带滤镜
};

1.KJRenderFilterTypeColor 色彩滤镜

这种滤镜的首要实现方法其实便是给图片加个色彩滤镜,就不做多余赘述

/// 色彩滤镜
- (UIImage *)kj_colorRenderInfo:(KJRenderInfo *)info{
    UIGraphicsBeginImageContext(self.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.originImage drawInRect:self.bounds];
    CGContextSetFillColorWithColor(context, [info.color colorWithAlphaComponent:info.extend].CGColor);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGContextFillRect(context, self.bounds);
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:imageRef];
    UIGraphicsEndImageContext();
    CGImageRelease(imageRef);
    return newImage;
}

2.KJRenderFilterTypeSketch 素描滤镜

其实中心仍是CoreImage的滤镜使用,先去色CIPhotoEffectMono,然后复制反色CIColorInvert,再高斯含糊CIGaussianBlur,最后混合叠加到去色图片CIColorDodgeBlendMode

/// 素描滤镜
- (void)kj_sketchRenderInfo:(KJRenderInfo *)info{
    _weakself;
    kGCD_QUEUE_ASYNC(^{
        /// 去色
        CIImage * inputImage = [[CIImage alloc] initWithImage:weakself.originImage];
        CIFilter * monoFilter = [CIFilter filterWithName:@"CIPhotoEffectMono"];
        [monoFilter setValue:inputImage forKey:kCIInputImageKey];
        CIImage * outImage = [monoFilter outputImage];
        /// 复制反色
        CIImage * invertImage = [outImage copy];
        CIFilter * invertFilter = [CIFilter filterWithName:@"CIColorInvert"];
        [invertFilter setValue:invertImage forKey:kCIInputImageKey];
        invertImage = [invertFilter outputImage];
        /// 高斯含糊
        CIFilter * blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
        [blurFilter setDefaults];
        [blurFilter setValue:@(info.extend*100) forKey:kCIInputRadiusKey];
        [blurFilter setValue:invertImage forKey:kCIInputImageKey];
        invertImage = [blurFilter valueForKey:kCIOutputImageKey];
        /// 混合叠加到去色图片
        CIFilter * blendFilter = [CIFilter filterWithName:@"CIColorDodgeBlendMode"];
        [blendFilter setValue:invertImage forKey:kCIInputImageKey];
        [blendFilter setValue:outImage forKey:kCIInputBackgroundImageKey];
        CIImage * sketchImage = [blendFilter outputImage];
        /// 烘托图片
        CGImageRef cgImage = [weakself.ciContext createCGImage:sketchImage fromRect:[inputImage extent]];
        kGCD_MAIN_ASYNC(^{
            weakself.image = [UIImage imageWithCGImage:cgImage];
            CGImageRelease(cgImage);
        });
    });
}

3.KJRenderFilterTypeCoreImage

CoreImage 滤镜,褐色滤镜,色彩反转

/// CoreImage 滤镜
typedef NS_OPTIONS(NSInteger, KJRenderCoreImageFilterType) {
    KJRenderCoreImageFilterTypeSepia = 0,  /// 褐色滤镜(怀旧)
    KJRenderCoreImageFilterTypeColorInvert,/// 色彩反转
};
static NSString * const _Nonnull KJRenderCoreImageFilterTypeStringMap[] = {
    [KJRenderCoreImageFilterTypeSepia]       = @"CISepiaTone",
    [KJRenderCoreImageFilterTypeColorInvert] = @"CIColorInvert",
};
static NSString * const _Nonnull KJRenderCoreImageFilterTypeKeyStringMap[] = {
    [KJRenderCoreImageFilterTypeSepia]       = @"inputIntensity",
    [KJRenderCoreImageFilterTypeColorInvert] = @"",
};
/// coreImage 滤镜
- (void)kj_coreImageRenderInfo:(KJRenderInfo *)info{
    _weakself;
    dispatch_queue_t globel = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(globel, ^{
        CIFilter *filter = [CIFilter filterWithName:KJRenderCoreImageFilterTypeStringMap[info.coreType]];
        [filter setDefaults];
        [filter setValue:[[CIImage alloc] initWithImage:weakself.originImage] forKey:kCIInputImageKey];
        NSString *string = KJRenderCoreImageFilterTypeKeyStringMap[info.coreType];
        if (![string isEqualToString:@""]) [filter setValue:@(info.extend) forKey:string];
        CIImage *outputImage  = [filter outputImage];
        CGImageRef filterimge = [weakself.ciContext createCGImage:outputImage fromRect:outputImage.extent];
        dispatch_queue_t main = dispatch_get_main_queue();
        dispatch_async(main, ^{
            weakself.image = [UIImage imageWithCGImage:filterimge];
            CGImageRelease(filterimge);
        });
    });
}

使用示例

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _weakself;
    CGFloat sp = kAutoW(10);
    CGFloat w = (kScreenW-sp*3)/2;
    CGFloat h = w;
    CGFloat maxY = 64.;
    UIImage *image  = kGetImage(@"IMG_4931");
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(sp, maxY+sp, w, h)];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.backgroundColor = [UIColor.orangeColor colorWithAlphaComponent:0.1];
    imageView.image = image;
    [self.view addSubview:imageView];
    __block NSArray <KJRenderInfo*>*renderTemps = @[({
        KJRenderInfo *info = [KJRenderInfo new];
        info.type = KJRenderFilterTypeOriginal;
        info.opacity = 1.;
        info;
    }),({
        KJRenderInfo *info = [KJRenderInfo new];
        info.type = KJRenderFilterTypeCoreImage;
        info.coreType = KJRenderCoreImageFilterTypeSepia;
        info.extend = .8;
        info.opacity = 1.;
        info;
    }),({
        KJRenderInfo *info = [KJRenderInfo new];
        info.type = KJRenderFilterTypeSketch;
        info.extend = .8;
        info.opacity = 1.;
        info;
    }),({
        KJRenderInfo *info = [KJRenderInfo new];
        info.type = KJRenderFilterTypeColor;
        info.extend = .8;
        info.color = [UIColor.greenColor colorWithAlphaComponent:0.1];
        info;
    })];
    KJRenderImageView *view = ({
        KJRenderImageView *renderImageView = [[KJRenderImageView alloc] kj_initWithOriImage:image ExtendParameterBlock:^(KJRenderImageView * _Nonnull obj) {
            obj.kAddView(weakself.view).kFrame(CGRectMake(sp+imageView.maxX, imageView.top, w, h));
        }];
        renderImageView.backgroundColor = [UIColor.orangeColor colorWithAlphaComponent:0.1];
        [renderImageView kj_changeRenderInfo:renderTemps[1]];
        renderImageView;
    });
    KJRenderImageView *view2 = ({
        KJRenderImageView *renderImageView = [[KJRenderImageView alloc] kj_initWithOriImage:image ExtendParameterBlock:^(KJRenderImageView * _Nonnull obj) {
            obj.kAddView(weakself.view).kFrame(CGRectMake(sp, view.bottom+sp, w, h));
        }];
        renderImageView.backgroundColor = [UIColor.orangeColor colorWithAlphaComponent:0.1];
        [renderImageView kj_changeRenderInfo:renderTemps[2]];
        renderImageView;
    });
    KJRenderImageView *view3 = ({
        KJRenderImageView *renderImageView = [[KJRenderImageView alloc] kj_initWithOriImage:image ExtendParameterBlock:^(KJRenderImageView * _Nonnull obj) {
            obj.kAddView(weakself.view).kFrame(CGRectMake(sp+view2.maxX, view.bottom+sp, w, h));
        }];
        renderImageView.backgroundColor = [UIColor.orangeColor colorWithAlphaComponent:0.1];
        [renderImageView kj_changeRenderInfo:renderTemps[3]];
        renderImageView;
    });
}

备注:本文用到的部分函数方法和Demo,均来自三方库KJCategories

滤镜介绍就到此完毕,后面有相关再弥补