Unable to convert CIImage to UIImage in Swift 3.0
Asked Answered
L

3

19

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

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

        let filter = CIFilter(name: "CIQRCodeGenerator")

        filter?.setValue(stringData, forKey: "inputMessage")

        filter?.setValue("H", forKey: "inputCorrectionLevel")

        return filter?.outputImage
    }

And Then I am adding to UIImageView Like this:

    if let img = createQRFromString(strQRData) {
        let somImage = UIImage(CIImage: img, scale: 1.0, orientation: UIImageOrientation.Down)
        imgviewQRcode.image = somImage
    }

Now I need to save this to a JPEG or PNG file. But when I am doing so my app crashes:

    @IBAction func btnSave(sender: AnyObject) {

    //        // Define the specific path, image name
    let documentsDirectoryURL = try! NSFileManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
    // create a name for your image
    let fileURL = documentsDirectoryURL.URLByAppendingPathComponent("image.jpg")

    if let image = imgviewQRcode.image // imgviewQRcode is UIImageView
    {

        if let path = fileURL?.path
        {
            if !NSFileManager.defaultManager().fileExistsAtPath(fileURL!.path!)
            {
                if UIImageJPEGRepresentation(image, 1.0)!.writeToFile(path, atomically: true)
                {
                    print("file saved")
                }

            }//Checking existing file

        }//Checking path

    }//CHecking image

}

Crash Point

  UIImageJPEGRepresentation(image, 1.0)!.writeToFile(path, atomically: true)

Reason

fatal error: unexpectedly found nil while unwrapping an Optional value

Debug Tests:

enter image description here

Linetta answered 2/12, 2016 at 18:49 Comment(0)
C
64
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 .

Crust answered 2/12, 2016 at 18:54 Comment(3)
@Shenona what should I pass in this methodLinetta
This method requires CIImage . As per the post you are having trouble converting CIImage to UIImage . This method is having parameter which requires CIImage and returns UIImage .Crust
@Shemona why this method is not releasing memory?Spouse
L
4

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
}

Lightner answered 22/12, 2017 at 19:47 Comment(0)
S
4
 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.

Seasoning answered 15/5, 2018 at 17:0 Comment(2)
Intriguing, but for some reason nil when I use this instead of context.createCGImage(myCIImage, from:rect)Photopia
I also get no output via UIImage(ciImage:), whereas I do get something from context.createCGImage()Problem

© 2022 - 2024 — McMap. All rights reserved.