Unable to convert CIImage to UIImage in Swift 3.0

后端 未结 3 521
栀梦
栀梦 2020-12-10 02:49

I am making image form QR Code by using following code:

  func createQRFromString(str: String) -> CIImage? {
        let stringData = str.dat         


        
相关标签:
3条回答
  • 2020-12-10 03:18

    My final code

       func generateQRCode(from string: String) -> UIImage? {
            let data = string.data(using: String.Encoding.ascii)
            
            if let filter = CIFilter(name: "CIQRCodeGenerator") {
                filter.setValue(data, forKey: "inputMessage")
                let transform = CGAffineTransform(scaleX: 3, y: 3)
                
                if let output = filter.outputImage?.transformed(by: transform) {
                    let context:CIContext = CIContext.init(options: nil)
                    let cgImage:CGImage = context.createCGImage(output, from: output.extent)!
                    let image:UIImage = UIImage.init(cgImage: cgImage)
                    return image
                }
            }
            
            return nil
        }

    0 讨论(0)
  • 2020-12-10 03:22
    func convert(cmage:CIImage) -> UIImage
    {       
        let context:CIContext = CIContext.init(options: nil)
        let cgImage:CGImage = context.createCGImage(cmage, from: cmage.extent)!        
        let image:UIImage = UIImage.init(cgImage: cgImage)        
        return image
    }
    

    Use this function to convert CIImage to UIImage . It works .

    0 讨论(0)
  • 2020-12-10 03:26
     func convert(image:CIImage) -> UIImage
     {           
        let image:UIImage = UIImage.init(ciImage: image)        
        return image
     }
    

    Perhaps, this was unavailable before, but it is now possible to create UIImages directly from CIImage.

    0 讨论(0)
提交回复
热议问题