美文网首页
ios图片滤镜

ios图片滤镜

作者: Arthur澪 | 来源:发表于2018-02-06 10:30 被阅读0次

    给图片添加滤镜,调整图片内容的颜色。

    - (void)addColorFilter{
        
        CIImage *inputImage = [CIImage imageWithCGImage:_imgV.image.CGImage];
        //先打印NSLog(@"%@",[CIFilter filterNamesInCategory:kCICategoryDistortionEffect]);进去找到需要设置的属性(查询效果分类中都有什么效果)  可以设置什么效果
        //然后通过[CIFilter filterWithName:@""];找到属性   具体效果的属性
        //然后通过KVC的方式设置属性
        NSLog(@"%@",[CIFilter filterNamesInCategory:kCICategoryDistortionEffect]);
        /*
         1.查询 效果分类中 包含什么效果:filterNamesInCategory:
         2.查询 使用的效果中 可以设置什么属性(KVC) attributes
         
         使用步骤
         1.需要添加滤镜的源图
         2.初始化一个滤镜 设置滤镜(根据查询到的属性来设置)
         3.把滤镜 输出的图像 和滤镜  合并 CIContext -> 得到一个合成之后的图像
         4.展示
         */
        CIFilter *filter = [CIFilter filterWithName:@"CIColorMonochrome"];
        NSLog(@"%@",filter.attributes);
        //这个属性是必须赋值的,假如你处理的是图片的话
        [filter setValue:inputImage forKey:kCIInputImageKey];
    
     // 通过改变颜色值,调整想要的颜色
        CIColor *color = [CIColor colorWithRed:0.759 green:1.00 blue:0.592 alpha:1];  
        [filter setValue:color forKey:kCIInputColorKey];
        //CIContext
        CIContext *context = [CIContext contextWithOptions:nil];
        
        CIImage *outPutImage = filter.outputImage;
        
        CGImageRef image = [context createCGImage:outPutImage fromRect:outPutImage.extent];
        
        _imgV.image = [UIImage imageWithCGImage:image];
        
    }
    

    改变hud

    - (void)changeHueAdjust{
        //  原图
        NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"manHair6"]);
        CIImage *_imgSource = [CIImage imageWithData:imageData];
        
       //创建基于GPU的CIContext对象实例
       CIContext *_context = [CIContext contextWithOptions: nil];
       //创建滤镜
       CIFilter *_filterHueAdjust = [CIFilter filterWithName:@"CIHueAdjust"];
        
        //设置过滤器参数
        [_filterHueAdjust setValue:_imgSource forKey:kCIInputImageKey];
         float slideValue = 0.5;   // 可变
        [_filterHueAdjust setValue:[NSNumber numberWithFloat: slideValue] forKey:@"inputAngle"];
        
        //得到过滤后的图片
        CIImage *imgOutput = [_filterHueAdjust outputImage];
        
        //转换图片
        CGImageRef imgRef = [_context createCGImage:imgOutput fromRect:[imgOutput extent]];
        UIImage *imgNew = [UIImage imageWithCGImage:imgRef];
        
        _imgV.image = imgNew;
        
       //释放CGImageRef对象实例
        CGImageRelease(imgRef);
    }
    

    工具方法——图像颜色滤镜

    功能描述:给定一个rgb,实现图片颜色的变化。
    1、提供外界调用方法

    - (UIImage *)addColorFilter:(UIImage *)img rgbDict:(NSDictionary *)rgb;
    

    2、实现
    添加全局的滤镜属性CIFilter

    @interface PDfilterTool()
    
    @property(nonatomic,strong)CIFilter *filter;   //滤镜1
    @property(nonatomic,strong)CIFilter *filter2;   //滤镜2
    
    @property(nonatomic,strong)CIContext *context;
    
    @end
    

    实现懒加载

    -(CIFilter *)filter{
        if(!_filter){
            _filter = [CIFilter filterWithName:@"CIColorMonochrome"];
        }
        return _filter;
    }
    -(CIFilter *)filter2{
        if(!_filter2){
            _filter2=[CIFilter filterWithName:@"CIColorControls"];
            [_filter2 setValue:[NSNumber numberWithFloat:1.2] forKey:@"inputContrast"];
    
        }
        return _filter2;
    }
    

    图片处理

    - (UIImage *)addColorFilter:(UIImage *)img rgbDict:(NSDictionary *)rgb{
        
        CIImage *inputImage = [CIImage imageWithCGImage:img.CGImage];
        [self.filter setValue:inputImage forKey:kCIInputImageKey];
        
        CIColor *color = [CIColor colorWithRed:[rgb[@"red"] floatValue] green:[rgb[@"green"] floatValue] blue:[rgb[@"blue"] floatValue] alpha:1];   // 改变颜色值
        [self.filter setValue:color forKey:kCIInputColorKey];
        CIImage *outPutImage = self.filter.outputImage;
        
        CGImageRef image = [self.context createCGImage:outPutImage fromRect:outPutImage.extent];
        
        return [UIImage imageWithCGImage:image];
        
    //--> +再次添加滤镜  形成滤镜链
    //    [self.filter2 setValue:outPutImage forKey:kCIInputImageKey];
    //    CGImageRef resultImage = [self.context createCGImage:_filter2.outputImage fromRect:_filter2.outputImage.extent];
    //    return  [UIImage imageWithCGImage:resultImage];
    }
    

    相关资料

    https://www.jianshu.com/p/98ba256b034a#comment-19205263
    https://www.cnblogs.com/fengmin/p/5586729.html
    https://www.jianshu.com/p/b58b7d4615ee
    https://www.jianshu.com/p/98ba256b034a
    https://www.cnblogs.com/wukun16/p/4884158.html

    相关文章

      网友评论

          本文标题:ios图片滤镜

          本文链接:https://www.haomeiwen.com/subject/pugknxtx.html