如何正确应用Core Image过滤器

时间:2012-06-22 00:15:46

标签: iphone objective-c ios ios5 core-image

我在使用Core图像时出现问题。我正在做的是从UIImageView获取一个图像,然后使用我在教程中找到的一些代码(我是核心图像的新手),但是当我尝试使用时,我想将棕褐色图像放回到相同的UIImageView中将新图像放入视图中它只是消失我已经测试过,看看图像视图是否包含图像但它确实存在,但它不可见。关于该做什么的任何建议?

编辑: 好吧,我得到棕褐色过滤器工作所以然后我尝试了posterize,我有同样的问题,图像刚刚消失。这是代码:

CIImage *beginImage = [CIImage imageWithCGImage:[image_view.image CGImage]];
context = [CIContext contextWithOptions:nil];
filter = [CIFilter filterWithName:@"CIColorPosterize" keysAndValues:kCIInputImageKey, beginImage,@"inputLevels",[NSNumber numberWithFloat:0.8], nil];
CIImage *outputImage = [filter outputImage];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newImg = [UIImage imageWithCGImage:cgimg];
[image_view setImage:newImg];
CGImageRelease(cgimg);

2 个答案:

答案 0 :(得分:11)

尝试这样的事情(假设你有一个UIImageView * myImageView)

CIImage *beginImage = [CIImage imageWithCGImage:[myImageView.image CGImage]];
CIContext *context = [CIContext contextWithOptions:nil];

CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone" keysAndValues: kCIInputImageKey, beginImage, @"inputIntensity", [NSNumber numberWithFloat:0.8], nil];
CIImage *outputImage = [filter outputImage];

CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newImg = [UIImage imageWithCGImage:cgimg];

[myImageView setImage:newImg];

CGImageRelease(cgimg);

答案 1 :(得分:0)

Xcode 8.2上的Swift 3.0中的相同代码(我的项目中的imageView是" @IBOutlet var photoImageView:UIImageView!") 在iPhone 7Plus设备iOS 10上成功检查

    guard let myImageView = self.photoImageView.image else
    {
        return
    }
    guard let cg = myImageView.cgImage else
    {
        return
    }
    let beginImage = CIImage(cgImage: cg)
    let context : CIContext = CIContext(options: nil)
    let inputParams : [String : Any]? = [kCIInputImageKey : beginImage,
                                        "inputIntensity" : NSNumber(value : 0.8)]
    let filter : CIFilter = CIFilter(name: "CISepiaTone", withInputParameters: inputParams)!
    let outputImage : CIImage? = filter.outputImage!

    if let outputImage = outputImage
    {
        let cgImage : CGImage? = context.createCGImage(outputImage, from: outputImage.extent)!

        if let cg = cgImage
        {
            let newImage : UIImage = UIImage(cgImage: cg)
            photoImageView.image = newImage
        }
    }