5

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

编辑:好的,我让棕褐色过滤器工作,所以我尝试了色调分离,我遇到了同样的问题,图像就消失了。这是代码:

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);
4

2 回答 2

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);
于 2012-06-22T00:28:44.837 回答
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
        }
    }
于 2017-02-16T09:47:03.620 回答