我收到一个错误Thread1:EXC_BAD_INSTRACTION。
我正在制作Swift应用程序,我想在我的应用程序中创建一个系统,该系统将图像上传到Django服务器。当我没有选择任何图像,而是点击了“发送”按钮以将图像发送到服务器时,就会发生此错误。
error
现在,PhotoController(用于系统)是
import Foundation
import MobileCoreServices
import UIKit
class PhotoController:UIViewController,UINavigationControllerDelegate,UIImagePickerControllerDelegate{
@IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var myImageView: UIImageView!
private var imagePicker:UIImagePickerController!
@IBAction func uploadButtonTapped(_ sender: Any) {
myImageUploadRequest()
}
override func viewDidLoad() {
super.viewDidLoad()
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.5
label.text = "Tap the PhotoSelect or Camera to upload a picture"
}
@IBAction func PhotoSelect(_ sender: Any) {
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary
self.present(myPickerController, animated: true, completion: nil)
}
@IBAction func Camera(_ sender: Any) {
let sourceType:UIImagePickerControllerSourceType = UIImagePickerControllerSourceType.camera
// カメラが利用可能かチェック
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
// インスタンスの作成
let cameraPicker = UIImagePickerController()
cameraPicker.sourceType = sourceType
cameraPicker.delegate = self
self.present(cameraPicker, animated: true, completion: nil)
}
else{
label.text = "error"
}
}
// 撮影が完了時した時に呼ばれる
func imagePickerController(_ imagePicker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
myImageView.contentMode = .scaleAspectFit
myImageView.image = pickedImage
}
//閉じる処理
imagePicker.dismiss(animated: true, completion: nil)
label.text = "Tap the Send to save a picture"
}
// 撮影がキャンセルされた時に呼ばれる
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
label.text = "Canceled"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func myImageUploadRequest()
{
let myUrl = NSURL(string: "http://localhost:8000/admin/accounts/post/");
let request = NSMutableURLRequest(url:myUrl! as URL);
request.httpMethod = "POST";
//ユーザーごとに割り振りたい
let param = [
"firstName" : "Sergey",
"lastName" : "Kargopolov",
"userId" : "9"
]
let boundary = generateBoundaryString()
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let imageData = UIImageJPEGRepresentation(myImageView.image!, 1)
if(imageData==nil) { return; }
request.httpBody = createBodyWithParameters(parameters: param, filePathKey: "file", imageDataKey: imageData! as NSData, boundary: boundary) as Data
myActivityIndicator.startAnimating();
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
// You can print out response object
print("******* response = \(response)")
// Print out reponse body
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("****** response data = \(responseString!)")
do {
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
DispatchQueue.main.async {
self.myActivityIndicator.stopAnimating()
self.myImageView.image = nil;
}
}catch
{
print(error)
}
}
task.resume()
}
func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, imageDataKey: NSData, boundary: String) -> NSData {
let body = NSMutableData();
if parameters != nil {
for (key, value) in parameters! {
body.appendString(string: "--\(boundary)\r\n")
body.appendString(string: "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString(string: "\(value)\r\n")
}
}
let filename = "user-profile.jpg"
let mimetype = "image/jpg"
body.appendString(string: "--\(boundary)\r\n")
body.appendString(string: "Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n")
body.appendString(string: "Content-Type: \(mimetype)\r\n\r\n")
body.append(imageDataKey as Data)
body.appendString(string: "\r\n")
body.appendString(string: "--\(boundary)--\r\n")
return body
}
func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().uuidString)"
}
}
extension NSMutableData {
func appendString(string: String) {
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
append(data!)
}
}
追溯是
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
我不明白为什么会发生此错误,因为我在这一部分中写了if语句,以表示意外情况
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
// You can print out response object
print("******* response = \(response)")
// Print out reponse body
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("****** response data = \(responseString!)")
do {
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
DispatchQueue.main.async {
self.myActivityIndicator.stopAnimating()
self.myImageView.image = nil;
}
}catch
{
print(error)
}
}
我该如何解决这个问题?我的目标是在我的应用中制作一个系统,将图像上传到Django服务器,即使发生所有意外错误也可以。(顺便说一句,当我没有选择任何图像时,我想显示警报,例如“ERROR”)
最佳答案
确定,因此您的应用程序在此处崩溃:
let imageData = UIImageJPEGRepresentation(myImageView.image!, 1)
这表明
myImageView.image
为nil,这可能是由多种原因引起的。myImageView
实际上没有图像。 myImageView
@IBOutlet未正确连接您在这里有支票:
if(imageData==nil) { return; }
但是,正如您所看到的,那只是一行,为时已晚:)
您可以做的是对
myImageView.image
进行解包,并且仅在出现某种情况时才继续操作,否则从函数中返回。看起来像这样:
guard let myImage = myImageView.image else {
return
}
现在,您知道图像具有有效值,并且可以安全地继续。
因此,您的整个函数将如下所示:
func myImageUploadRequest()
{
let myUrl = NSURL(string: "http://localhost:8000/admin/accounts/post/");
let request = NSMutableURLRequest(url:myUrl! as URL);
request.httpMethod = "POST";
//ユーザーごとに割り振りたい
let param = [
"firstName" : "Sergey",
"lastName" : "Kargopolov",
"userId" : "9"
]
let boundary = generateBoundaryString()
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
guard let myImage = myImageView.image else {
return
}
let imageData = UIImageJPEGRepresentation(myImage, 1)
if(imageData==nil) { return; }
request.httpBody = createBodyWithParameters(parameters: param, filePathKey: "file", imageDataKey: imageData! as NSData, boundary: boundary) as Data
myActivityIndicator.startAnimating();
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
// You can print out response object
print("******* response = \(response)")
// Print out reponse body
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("****** response data = \(responseString!)")
do {
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
DispatchQueue.main.async {
self.myActivityIndicator.stopAnimating()
self.myImageView.image = nil;
}
}catch
{
print(error)
}
}
task.resume()
}
希望对您有帮助。