我通过将一些详细信息发送到服务器来生成QRCode的任务,后端人员将生成哈希ID。通过使用此哈希ID,我需要生成QRCode并将其从一个viewcontroller传递到另一个viewcontroller。
我能够获取QRCode图像。但是,在将其解析到另一个视图控制器时,出现了类似错误
在线程1:致命错误:展开包装时意外发现nil 可选值
productdetails.qrimgobj.image = imgobj.image
行中。如果有人帮助我传递图像,那就太好了,谢谢您。
@IBAction func generateQRCode(_ sender: Any) {
type = "0"
getqrandimage()
}
func getqrandimage(){
name = nametxt.text!
phonenum = phonenumbertxt.text!
landnum = landlinetxt.text!
email = emailtxt.text!
web = websitetxt.text!
desc = descriptiontxt.text!
price = pricetxt.text!
offer = offers.text!
let acce:String = UserDefaults.standard.string(forKey: "access-tokenn")!
print(acce)
let headers:HTTPHeaders = ["Authorization":"Bearer \(acce)"]
parameter = ["type":type,"name":name,"plus_code":pluscode,"latitude":lat,"longitude":long,"location":address,"phone":phonenum,"isd_code":isd,"landline":landnum,"std_code":std,"email":email,"website":web,"description":desc,"price":price,"offer":offer]
// imgobj.image=UIImage(named: "Mahi.png")
let imageobj = UIImage(named: "Mahi.png")!
let ImageData = imageobj.jpegData(compressionQuality: 0.5)
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(ImageData!, withName: "photo", fileName: self.filename, mimeType: "image/jpeg")
for (key, value) in self.parameter {
multipartFormData.append(self.type.data(using: String.Encoding.utf8)!,withName: "type")
multipartFormData.append(self.name.data(using: String.Encoding.utf8)!,withName: "name")
multipartFormData.append(self.pluscode.data(using: String.Encoding.utf8)!,withName: "plus_code")
multipartFormData.append(self.str1.data(using: String.Encoding.utf8)!,withName: "latitude")
multipartFormData.append(self.str2.data(using: String.Encoding.utf8)!,withName: "longitude")
multipartFormData.append(self.address.data(using: String.Encoding.utf8)!,withName: "location")
multipartFormData.append(self.phonenum.data(using: String.Encoding.utf8)!,withName: "phone")
multipartFormData.append(self.isd.data(using: String.Encoding.utf8)!,withName: "isd_code")
multipartFormData.append(self.landnum.data(using: String.Encoding.utf8)!,withName: "landline")
multipartFormData.append(self.std.data(using: String.Encoding.utf8)!,withName: "std_code")
multipartFormData.append(self.email.data(using: String.Encoding.utf8)!,withName: "email")
multipartFormData.append(self.web.data(using: String.Encoding.utf8)!,withName: "website")
multipartFormData.append(self.desc.data(using: String.Encoding.utf8)!,withName: "description")
multipartFormData.append(self.price.data(using: String.Encoding.utf8)!,withName: "price")
multipartFormData.append(self.offer.data(using: String.Encoding.utf8)!,withName: "offer")
}
},to:Constants.productcreate,
method: .post,
headers: headers)
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (Progress) in
print("Upload Progress: \(Progress.fractionCompleted)")
})
upload.responseJSON { response in
//self.delegate?.showSuccessAlert()
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
// self.showSuccesAlert()
//self.removeImage("frame", fileExtension: "txt")
if let JSON = response.result.value {
// print("JSON: \(JSON)")
var responsedict:Dictionary = response.result.value as! [String:Any]
print(responsedict)
var subdic:[String:Any] = responsedict["data"] as! [String:Any]
print(subdic)
var dict:[String:Any] = subdic["product"] as! [String:Any]
print(dict)
self.qrid = dict["hashed_id"] as! String
print("qrcode\(self.qrid)")
self.qrgenerate()
}
}
case .failure(let encodingError):
//self.delegate?.showFailAlert()
print(encodingError)
}
}
}
func qrgenerate(){
let data = qrid.data(using: .ascii, allowLossyConversion: false)
let filter = CIFilter(name: "CIQRCodeGenerator")
filter?.setValue(data, forKey: "InputMessage")
let ciImage = filter?.outputImage
let transform = CGAffineTransform(scaleX: 10, y: 10)
let transformimage = ciImage?.transformed(by: transform)
qrim = UIImage(ciImage: transformimage!)
imgobj.image=qrim
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let productdetails = storyboard.instantiateViewController(withIdentifier: "LocatemypicViewController") as! LocatemypicViewController
print(imgobj.image)
productdetails.qrimgobj.image = imgobj.image
self.navigationController?.pushViewController(productdetails, animated: true)
}
答案 0 :(得分:0)
您在这里崩溃:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let productdetails = storyboard.instantiateViewController(withIdentifier: "LocatemypicViewController") as! LocatemypicViewController
print(imgobj.image)
productdetails.qrimgobj.image = imgobj.image
特别是在此行中:productdetails.qrimgobj.image = imgobj.image
...这是因为您试图访问或更确切地说将对象(UIImage
)传递给qrimgobj
到我认为是{{ 1}},而该UIImageView
属性尚未初始化。您可以做的一件事情是拥有一个属性,该属性可以临时保存图像,然后在整个屏幕加载后将其传递给qrimgobj
。
示例:
qrimgobj