簡體   English   中英

無法在 Swift 3.0 中將 CIImage 轉換為 UIImage

[英]Unable to convert CIImage to UIImage in Swift 3.0

我正在使用以下代碼制作圖像形式的QR 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
    }

然后我像這樣添加到UIImageView

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

現在我需要將其保存為JPEGPNG文件。 但是當我這樣做時,我的應用程序崩潰了:

    @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

}

崩潰點

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

原因

fatal error: unexpectedly found nil while unwrapping an Optional value

調試測試:

在此處輸入圖像描述

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
}

使用此函數將 CIImage 轉換為 UIImage 。 有用 。

我的最終代碼

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
}

 func convert(image:CIImage) -> UIImage
 {           
    let image:UIImage = UIImage.init(ciImage: image)        
    return image
 }

也許,這在以前是不可用的,但現在可以直接從 CIImage 創建 UIImages。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM