[英]How to pass an image from one viewcontroller to another viewcontroller in swift?
My task in generating QRCode by sending some details to server, backend people will generate hashed id.我的任务是通过向服务器发送一些详细信息来生成 QRCode,后端人员将生成散列 ID。 By using this hashed id, I need to generate QRCode and pass it from one viewcontroller to another viewcontroller.
通过使用这个散列 id,我需要生成 QRCode 并将其从一个视图控制器传递到另一个视图控制器。
I am able to get QRCode image.But ,while parsing it to another view controller I am getting an error like我能够获得 QRCode 图像。但是,在将其解析到另一个视图控制器时,我收到了类似的错误
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
线程 1:致命错误:在展开 Optional 值时意外发现 nil
at the line productdetails.qrimgobj.image = imgobj.image
.在
productdetails.qrimgobj.image = imgobj.image
。 If anyone helps me to pass images would be great, and thank you in advance.如果有人帮助我传递图像会很棒,并在此先感谢您。
@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)
}
You're getting a crash here:你在这里崩溃了:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let productdetails = storyboard.instantiateViewController(withIdentifier: "LocatemypicViewController") as! LocatemypicViewController
print(imgobj.image)
productdetails.qrimgobj.image = imgobj.image
Specifically in this line: productdetails.qrimgobj.image = imgobj.image
... This is because you are trying to access or rather passing an object ( UIImage
) to your qrimgobj
which I presume is a UIImageView
while that qrimgobj
property is not yet initialized.特别是在这一行:
productdetails.qrimgobj.image = imgobj.image
... 这是因为您正在尝试访问或将对象( UIImage
)传递给您的qrimgobj
,我认为它是一个UIImageView
而该qrimgobj
属性尚未初始化. One of the things you can do is to have a property that holds the image temporarily and then pass it to qrimgobj
after the whole screen is loaded.您可以做的一件事是拥有一个临时保存图像的属性,然后在整个屏幕加载后将其传递给
qrimgobj
。
Example:例子:
var qrimage: UIImage?
viewDidLoad() {
super.viewDidLoad()
self.qrimgobj.image = self.qrimage // Pass
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.