线程1:EXC_BREAKPOINT(代码= 1,子代码= 0x18407cb5c)

时间:2018-07-13 23:20:11

标签: ios swift qr-code

我不断收到此错误:

  

线程1:EXC_BREAKPOINT(代码= 1,子代码= 0x18407cb5c)

每当我运行此代码时:

import UIKit
import SQLite

class TakeMoney: UIViewController {
    //for json
    let json = "[{"+"\"UserID\":\""+"\(Int(stored_ID) ?? -1)\""+",\"Name\":"+"\"\(stored_name)\""+"}]"
    //
    var img : UIImage? = nil

    //variable for image display space
    @IBOutlet weak var ImageDisplay: UIImageView!
    //variable for raw qr image output
    var qrCodeImage : CIImage!
    override func viewDidLoad() {
        super.viewDidLoad()
        print(json)
        ////takes out the input and encodes it
        ///runs on main thread
        DispatchQueue.main.async{
            let data = self.json.data(using: String.Encoding.isoLatin1, allowLossyConversion: false)
            let filter = CIFilter(name: "CIQRCodeGenerator") //defining filter
            filter?.setValue(data, forKey: "inputMessage") //utilising filter
            filter?.setValue("L", forKey: "inputCorrectionLevel") //defining the level of quality, H is highest, L is lowest
            self.qrCodeImage = filter?.outputImage //getting the filter to output raw image
            //to set the size of qr code
            let length = self.ImageDisplay.frame.size.width
            let height = self.ImageDisplay.frame.size.height
            let transformedImage = self.qrCodeImage.transformed(by: CGAffineTransform(scaleX: length, y: height))
            //
            self.img = UIImage(ciImage: transformedImage)
            print(self.img!)
            if(self.img == nil){
                print("It is empty")
            }
            else{
                self.ImageDisplay.image = self.img
            }
        }
    }
}

该代码应采用JSON值并将其转换为QR码,然后将通过“ ImageDisplay”图像视图元素显示该QR码。当我在模拟器上运行它时,它可以工作,但是当我在手机上运行它时,它却无法工作,并且总是崩溃,从而产生前面所述的错误。

它在必须将UIImage分配给图像视图的行上崩溃:

self.ImageDisplay.image = self.img

2 个答案:

答案 0 :(得分:0)

self.ImageDisplay最有可能是零。由于它是一个隐式解包的可选内容,因此如果它为nil,则访问它将会崩溃。

您需要确保已将其连接到情节提要/ xib中。

答案 1 :(得分:-1)

在实际检查print(self.img!)中的nil是否为nil之前,您必须强制解开该值。

力量解开很可能返回nil,从而导致应用崩溃。