Xcode - 多个照片滤镜

时间:2014-08-10 10:24:28

标签: ios xcode filter photo core-image

我正在创建一个涉及过滤器的照片应用。过滤器工作,但它们保持永久性并相互重叠。例如,我想要从图像中删除棕褐色滤镜,并在按下即时滤镜按钮时用即时滤镜替换。我该如何实现这一目标?以下是该项目的一些代码:

- (IBAction)sepiaFilter:(id)sender {

    CIImage *beginImage = [CIImage imageWithData: UIImagePNGRepresentation(self.picture.image)];
    CIContext *context = [CIContext contextWithOptions:nil];
    CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone" keysAndValues:kCIInputImageKey, beginImage, @"inputIntensity", [NSNumber numberWithFloat:0.8], nil ];
    CIImage *outputImage = [filter outputImage];
    CGImageRef cging = [context createCGImage:outputImage fromRect:[outputImage extent]];
    self.picture.image = [UIImage imageWithCGImage:cging];
    CGImageRelease(cging);     
}

- (IBAction)instantFilter:(id)sender {

    CIImage *beginImage = [CIImage imageWithData: UIImagePNGRepresentation(self.picture.image)];
    CIContext *context = [CIContext contextWithOptions:nil];
    CIFilter *filterTwo = [CIFilter filterWithName:@"CIPhotoEffectInstant" keysAndValues:kCIInputImageKey, beginImage, nil ];
    CIImage *outputImage = [filterTwo outputImage];
    CGImageRef cging = [context createCGImage:outputImage fromRect:[outputImage extent]];
    self.picture.image = [UIImage imageWithCGImage:cging];
    CGImageRelease(cging);
}

(我实际上有九个过滤器,但我只需要知道如何做到这一点)感谢您的帮助

1 个答案:

答案 0 :(得分:0)

非常简单。您正在使用self.picture.image作为输入和输出。分别保留原始UIImage用于输入,并仅将self.picture.image用于输出,因此代码中唯一更改的是两个过滤器的第一行,即

CIImage *beginImage = [CIImage imageWithData: UIImagePNGRepresentation(self.originalImage)];

希望这有帮助。