API & Property

/// initialize - (instancetype)kj_initWithOriImage:(UIImage *)image ExtendParameterBlock:(void(^_Nullable)(KJRenderImageView) *obj))paramblock; // change effect - (void)kj_changeRenderInfo:(KJRenderInfo *)info; @interface KJRenderInfo : NSObject @property(nonatomic,assign)KJRenderFilterType type; @property(nonatomic,assign)float opacity; /// Transparency, default 1 @property(nonatomic,assign)float extend; /// extend @property(nonatomic,strong)UIColor *color; / / / color filter @ property (nonatomic, assign) KJRenderCoreImageFilterType coreType; // CoreImage filter @endCopy the code

A brief introduction

KJRenderInfo Filter model data

There are currently four filters

Typedef NS_OPTIONS (NSInteger KJRenderFilterType) {KJRenderFilterTypeOriginal, / / / the raw KJRenderFilterTypeColor, / / / color filter KJRenderFilterTypeSketch, drawing filter KJRenderFilterTypeCoreImage / / / / / / CoreImage cabin filter};Copy the code

1. KJRenderFilterTypeColor color filters

The main way to implement this filter is to add a color filter to the image, so I won’t go into the details

/ / / color filters - (UIImage *) kj_colorRenderInfo info: (KJRenderInfo *) {UIGraphicsBeginImageContext (self. The 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; }Copy the code

2. KJRenderFilterTypeSketch sketch filter

In fact, the core or CoreImage filter is used, first remove color CIPhotoEffectMono, then copy the reverse color CIColorInvert, then gaussian blur, and finally mix and overlay to remove color picture CIColorDodgeBlendMode

Kj_sketchRenderInfo :(KJRenderInfo *)info{_weakself; KGCD_QUEUE_ASYNC (^ {/ / / to color 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]; / / / rendered image CGImageRef cgImage = [weakself. CiContext createCGImage: sketchImage fromRect: [inputImage among]]. kGCD_MAIN_ASYNC(^{ weakself.image = [UIImage imageWithCGImage:cgImage]; CGImageRelease(cgImage); }); }); }Copy the code

3.KJRenderFilterTypeCoreImage

CoreImage filter, Brown filter, color inversion

/ / / CoreImage filters typedef NS_OPTIONS (NSInteger KJRenderCoreImageFilterType) {KJRenderCoreImageFilterTypeSepia = 0, / / / brown filter (old) KJRenderCoreImageFilterTypeColorInvert, / / / color reversal}; static NSString * const _Nonnull KJRenderCoreImageFilterTypeStringMap[] = { [KJRenderCoreImageFilterTypeSepia] = @"CISepiaTone", [KJRenderCoreImageFilterTypeColorInvert] = @"CIColorInvert", }; static NSString * const _Nonnull KJRenderCoreImageFilterTypeKeyStringMap[] = { [KJRenderCoreImageFilterTypeSepia] = @"inputIntensity", [KJRenderCoreImageFilterTypeColorInvert] = @"", };Copy the code
// coreImage filter - (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); }); }); }Copy the code

Use the sample

- (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. The 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; }); }Copy the code

Note: Some of the function methods and Demo used in this article are from the tripartite library KJCategories

This is the end of the filter introduction, there are related to supplement