Nicht in der Lage zu konvertieren CIImage zu UIImage in Swift 3.0

Ich bin Bild-form QR Code mit den folgenden 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
    }

Und Dann bin ich noch hinzufügen UIImageView Wie diese:

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

Nun muss ich speichern Sie diese auf einem JPEG oder PNG - Datei. Aber, wenn ich Tue, damit meine app-Abstürze:

    @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)

Grund

fatal error: unexpectedly found nil while unwrapping an Optional value

Debug-Tests:

Nicht in der Lage zu konvertieren CIImage zu UIImage in Swift 3.0

InformationsquelleAutor Dalvik | 2016-12-02
Schreibe einen Kommentar