UIImage添加滤镜

UIImage *image =[UIImage imageNamed:@"1"];
 NSArray *arr = [NSArray arrayWithObjects:@"CISRGBToneCurveToLinear", @"CIVignetteEffect", @"Original", @"CIPhotoEffectNoir", @"CIPhotoEffectInstant",
     @"CIPhotoEffectProcess", @"CIPhotoEffectFade", @"CIPhotoEffectTonal", @"CIPhotoEffectMono", @"CIPhotoEffectChrome", nil];//oc默认的滤镜名称

imgView.image = [image addFilter:arr[i] ];

给uiimage 添加类别
- (UIImage *)addFilter:(NSString *)filterName
{
    if([filterName isEqualToString:@"Original"]){
        return self;
    }
    
    //将UIImage转换成CIImage
    CIImage *ciImage = [[CIImage alloc] initWithImage:self];
    
    //创建滤镜
    CIFilter *filter = [CIFilter filterWithName:filterName keysAndValues:kCIInputImageKey, ciImage, nil];
    
    //已有的值不变,其他的设为默认值
    [filter setDefaults];
    
    //获取绘制上下文
    CIContext *context = [CIContext contextWithOptions:nil];
    
    //渲染并输出CIImage
    CIImage *outputImage = [filter outputImage];
    
    //创建CGImage句柄
    CGImageRef cgImage = [context createCGImage:outputImage fromRect:[outputImage extent]];
    
    //获取图片
    UIImage *image = [UIImage imageWithCGImage:cgImage];
    
    //释放CGImage句柄
    CGImageRelease(cgImage);
    
    NSLog(@"%f   %f  %@",image.size.width,image.size.height,image);
    
    return image;
}

 

posted @ 2017-02-03 16:53  新年新气象  阅读(394)  评论(0编辑  收藏  举报