将UIImage转换为灰度,保持图像质量

时间:2016-10-21 14:09:38

标签: swift uiimage grayscale

我有此扩展程序(位于obj-c中,我将其转换为Swift3)以获得相同的UIImage但灰度级:

public func getGrayScale() -> UIImage
{
    let imgRect = CGRect(x: 0, y: 0, width: width, height: height)

    let colorSpace = CGColorSpaceCreateDeviceGray()

    let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue).rawValue)
    context?.draw(self.cgImage!, in: imgRect)

    let imageRef = context!.makeImage()
    let newImg = UIImage(cgImage: imageRef!)

    return newImg
}

我可以看到灰色图像,但它的质量非常糟糕......在上下文构造函数中,我唯一可以看到与质量相关的是bitsPerComponent: 8。无论如何看看Apple的文档,我得到的是:

enter image description here

它表明iOS只支持8bpc ......那么为什么我不能提高质量呢?

7 个答案:

答案 0 :(得分:29)

尝试以下代码:

注意:代码已更新且错误已修复...

  • 在Swift 3中测试的代码。
  • originalImage是您尝试转换的图片。

回答1:

     var context = CIContext(options: nil)

更新: CIContext是处理rendering的核心图像组件,核心图像的所有处理都在CIContext完成。这有点类似于Core GraphicsOpenGL context。有关详情,请参阅Apple Doc.

     func Noir() {

        let currentFilter = CIFilter(name: "CIPhotoEffectNoir") 
        currentFilter!.setValue(CIImage(image: originalImage.image!), forKey: kCIInputImageKey)
        let output = currentFilter!.outputImage 
        let cgimg = context.createCGImage(output!,from: output!.extent)
        let processedImage = UIImage(cgImage: cgimg!)
        originalImage.image = processedImage
      }

您还需要考虑以下可产生类似效果的过滤器

  • CIPhotoEffectMono
  • CIPhotoEffectTonal

答案1的输出:

enter image description here

答案2的输出:

enter image description here

改进答案:

答案2:在应用coreImage过滤器之前自动调整输入图像

var context = CIContext(options: nil)

func Noir() {


    //Auto Adjustment to Input Image
    var inputImage = CIImage(image: originalImage.image!)
    let options:[String : AnyObject] = [CIDetectorImageOrientation:1 as AnyObject]
    let filters = inputImage!.autoAdjustmentFilters(options: options)

    for filter: CIFilter in filters {
       filter.setValue(inputImage, forKey: kCIInputImageKey)
   inputImage =  filter.outputImage
      }
    let cgImage = context.createCGImage(inputImage!, from: inputImage!.extent)
    self.originalImage.image =  UIImage(cgImage: cgImage!)

    //Apply noir Filter
    let currentFilter = CIFilter(name: "CIPhotoEffectTonal") 
    currentFilter!.setValue(CIImage(image: UIImage(cgImage: cgImage!)), forKey: kCIInputImageKey)

    let output = currentFilter!.outputImage 
    let cgimg = context.createCGImage(output!, from: output!.extent)
    let processedImage = UIImage(cgImage: cgimg!)
    originalImage.image = processedImage

}

注意:如果您想看到更好的结果。您应该在real device上测试您的代码而不是simulator ...

答案 1 :(得分:15)

Swift 4.0 扩展程序,返回一个可选的UIImage,以避免任何潜在的崩溃。

import UIKit

extension UIImage {
    var noir: UIImage? {
        let context = CIContext(options: nil)
        guard let currentFilter = CIFilter(name: "CIPhotoEffectNoir") else { return nil }
        currentFilter.setValue(CIImage(image: self), forKey: kCIInputImageKey)
        if let output = currentFilter.outputImage,
            let cgImage = context.createCGImage(output, from: output.extent) {
            return UIImage(cgImage: cgImage, scale: scale, orientation: imageOrientation)
        }
        return nil
    }
}

要使用它:

let image = UIImage(...)
let noirImage = image.noir // noirImage is an optional UIImage (UIImage?)

答案 2 :(得分:9)

Joe的答案为UIImage Swift 4张力,extension UIImage { var noir: UIImage { let context = CIContext(options: nil) let currentFilter = CIFilter(name: "CIPhotoEffectNoir")! currentFilter.setValue(CIImage(image: self), forKey: kCIInputImageKey) let output = currentFilter.outputImage! let cgImage = context.createCGImage(output, from: output.extent)! let processedImage = UIImage(cgImage: cgImage, scale: scale, orientation: imageOrientation) return processedImage } } 正确地针对不同规模工作:

button-custom

答案 3 :(得分:4)

这是目标c中的一个类别。请注意,关键是,此版本需要考虑规模。

- (UIImage *)grayscaleImage{
    return [self imageWithCIFilter:@"CIPhotoEffectMono"];
}

- (UIImage *)imageWithCIFilter:(NSString*)filterName{
    CIImage *unfiltered = [CIImage imageWithCGImage:self.CGImage];
    CIFilter *filter = [CIFilter filterWithName:filterName];
    [filter setValue:unfiltered forKey:kCIInputImageKey];
    CIImage *filtered = [filter outputImage];
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef cgimage = [context createCGImage:filtered fromRect:CGRectMake(0, 0, self.size.width*self.scale, self.size.height*self.scale)];
    // Do not use initWithCIImage because that renders the filter each time the image is displayed.  This causes slow scrolling in tableviews.
    UIImage *image = [[UIImage alloc] initWithCGImage:cgimage scale:self.scale orientation:self.imageOrientation];
    CGImageRelease(cgimage);
    return image;
}

答案 4 :(得分:3)

我使用CoreImage,这可能会保持质量。

func convertImageToBW(image:UIImage) -> UIImage {

    let filter = CIFilter(name: "CIPhotoEffectMono")

    // convert UIImage to CIImage and set as input

    let ciInput = CIImage(image: image)
    filter?.setValue(ciInput, forKey: "inputImage")

    // get output CIImage, render as CGImage first to retain proper UIImage scale

    let ciOutput = filter?.outputImage
    let ciContext = CIContext()
    let cgImage = ciContext.createCGImage(ciOutput!, from: (ciOutput?.extent)!)

    return UIImage(cgImage: cgImage!)
}

根据您使用此代码的方式,出于性能原因,您可能希望在其外部创建CIContext。

答案 5 :(得分:1)

保存方向和规模:

extension UIImage {

func noir() -> UIImage {
    let context = CIContext(options: nil)

    let currentFilter = CIFilter(name: "CIPhotoEffectNoir")
    currentFilter!.setValue(CIImage(image: self), forKey: kCIInputImageKey)
    let output = currentFilter!.outputImage
    let cgimg = context.createCGImage(output!, from: output!.extent)
    let processedImage = UIImage(cgImage: cgimg!, scale: scale, orientation: imageOrientation)
    return processedImage
}}

答案 6 :(得分:0)

根据 Joe 回答,我们很容易将Original转换为B& W.但返回原始图片请参阅以下代码:

var context = CIContext(options: nil)
var startingImage : UIImage = UIImage()

func Noir() {     
    startingImage = imgView.image!
    var inputImage = CIImage(image: imgView.image!)!
    let options:[String : AnyObject] = [CIDetectorImageOrientation:1 as AnyObject]
    let filters = inputImage.autoAdjustmentFilters(options: options)

    for filter: CIFilter in filters {
        filter.setValue(inputImage, forKey: kCIInputImageKey)
        inputImage =  filter.outputImage!
    }
    let cgImage = context.createCGImage(inputImage, from: inputImage.extent)
    self.imgView.image =  UIImage(cgImage: cgImage!)

    //Filter Logic
    let currentFilter = CIFilter(name: "CIPhotoEffectNoir")
    currentFilter!.setValue(CIImage(image: UIImage(cgImage: cgImage!)), forKey: kCIInputImageKey)

    let output = currentFilter!.outputImage
    let cgimg = context.createCGImage(output!, from: output!.extent)
    let processedImage = UIImage(cgImage: cgimg!)
    imgView.image = processedImage
}

func Original(){ 
    imgView.image = startingImage
}