Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
982 views
in Technique[技术] by (71.8m points)

swift - PNG/JPEG representation from CIImage always returns nil

I'm currently making a photo editing app.

When a photo is selected by the user, it is automatically converted into black and white using this code:

func blackWhiteImage(image: UIImage) -> Data {
    print("Starting black & white")

    let orgImg = CIImage(image: image)
    let bnwImg = orgImg?.applyingFilter("CIColorControls", withInputParameters: [kCIInputSaturationKey:0.0])

    let outputImage = UIImage(ciImage: bnwImg!)

    print("Black & white complete")

    return UIImagePNGRepresentation(outputImage)!
}

The problem I am having with this code is that I keep getting this error:

fatal error: unexpectedly found nil while unwrapping an Optional value

I have had my code in a slightly different configuration, but it still breaks when it gets to the UIImagePNG/JPEGRepresentation(xx) section.

Are there any ways to get the PNG or JPEG data from a CIImage for use in an image view / just UIImage in general?

Any of the other methods don't go into enough detail for what code should be used.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just begin a new graphics context and draw your grayscale image there. iOS 10 or later you can use UIGraphicsImageRenderer, for older iOS version syntax please check edit history:

Xcode 11 ? Swift 5.1

func blackWhiteImage(image: UIImage, isOpaque: Bool = false) -> Data? {
    guard let ciImage = CIImage(image: image)?.applyingFilter("CIColorControls", parameters: [kCIInputSaturationKey: 0]) else { return nil }
    let format = image.imageRendererFormat
    format.opaque = isOpaque
    return UIGraphicsImageRenderer(size: image.size, format: format).image { _ in
        UIImage(ciImage: ciImage).draw(in: CGRect(origin: .zero, size: image.size))
    }.pngData()
}

You can also extend UIImage to return a grayscale image :

extension UIImage {
    var ciImage: CIImage? { CIImage(image: self) }
    func grayscale(isOpaque: Bool = false) -> UIImage? {
        guard let ciImage = ciImage?.applyingFilter("CIColorControls", parameters: [kCIInputSaturationKey: 0]) else { return nil }
        let format = imageRendererFormat
        format.opaque = isOpaque
        return UIGraphicsImageRenderer(size: size, format: format).image { _ in
             UIImage(ciImage: ciImage).draw(in: CGRect(origin: .zero, size: size))
        }
    }
}

let profilePicture = UIImage(data: try! Data(contentsOf: URL(string:"http://i.stack.imgur.com/Xs4RX.jpg")!))!

if let grayscale = profilePicture.grayscale(), let data = grayscale.pngData() {  // or Swift 4.1 or earlier -> let data = UIImagePNGRepresentation(grayscale)
    print(data.count)   //  689035
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...