PNG/JPEG representation from CIImage always returns nil

后端 未结 1 762
余生分开走
余生分开走 2020-11-29 12:35

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:

         


        
相关标签:
1条回答
  • 2020-11-29 13:08

    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
    }
    
    0 讨论(0)
提交回复
热议问题