CIFilter实现简单滤镜效果

CIFilter实现简单滤镜效果_第1张图片
Simulator Screen Shot.png
- (void)setupData {
    // 定义效果
    self.ljNamesArray = @[@"Original",@"CILinearToSRGBToneCurve",@"CIPhotoEffectChrome",@"CIPhotoEffectFade",@"CIPhotoEffectInstant",@"CIPhotoEffectMono",@"CIPhotoEffectProcess",@"CIPhotoEffectTonal",@"CIPhotoEffectTransfer"].mutableCopy;
    self.effectNameArray = @[@"Original",@"Curve",@"Chrome",@"Fade",@"Instant",@"Mono",@"Process",@"Tonal",@"Transfer"].mutableCopy;
    self.originalImage = [UIImage imageNamed:@"200922723503567.jpg"];
    for (int i = 0; i < self.ljNamesArray.count; i++) {
        UIImage *image = [LEAuxiliary changeImage:self.originalImage withIndex:i effectArray:self.ljNamesArray.copy];
        [self.ljImagesArray addObject:image];
    }
    
}

实现

+ (UIImage *)changeImage:(UIImage *)originalImage withIndex:(int)index effectArray:(NSArray *)effectArray {
    switch (index) {
        case 0:
        {
            return originalImage;
        }
            break;
        default:
        {
            return  [self Image:originalImage withEffect:effectArray[index]];
        }
            break;
    }

}

+ (UIImage *)Image:(UIImage *)image withEffect:(NSString *)effect {
    CIImage *ciImage = [[CIImage alloc] initWithImage:image];
    CIFilter *filter = [CIFilter filterWithName:effect keysAndValues:kCIInputImageKey, ciImage, nil];
    
    [filter setDefaults];
    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *outputImage = [filter outputImage];
    CGImageRef cgImage = [context createCGImage:outputImage fromRect:[outputImage extent]];
    UIImage *effetImage = [UIImage imageWithCGImage:cgImage];
    
    CGImageRelease(cgImage);
    return effetImage;
}

下方效果点击切换TableView创建

- (void)sharedInit {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.imageView.frame), 100, SCREEN_WIDTH) style:UITableViewStylePlain];
    self.tableView.center = CGPointMake(self.view.frame.size.width * 0.5, self.view.frame.size.height - 100);
    self.tableView.delegate         = self;
    self.tableView.dataSource       = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.transform        = CGAffineTransformMakeRotation(-M_PI / 2);
    self.tableView.separatorStyle   = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundView   = nil;
    [self.view addSubview:self.tableView];
}

点击切换

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  self.imageView.image = [LEAuxiliary changeImage:self.originalImage withIndex:(int)indexPath.row effectArray:self.ljNamesArray.copy];
}

你可能感兴趣的:(CIFilter实现简单滤镜效果)