2015-11-20 61 views
1

我試圖讓這個貼子的應用程序類似於instagram,但一旦我按下我的撰寫按鈕,我就陷入了一個問題。這個按鈕是假設發佈的圖像,而不是它的崩潰。我有一個錯誤後,我嘗試在這裏與我的應用程序上傳字幕\圖像:Swift Error:EXE_BAD_INSTRUCTION(code = EXC_I386_INVOP,subcode = 0x0)

let imageData = UIImagePNGRepresentation(imageToBeUploaded!)! 

這是給我這個錯誤:EXE_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

請幫幫忙!

@IBOutlet weak var captionTextView: UITextView! 
@IBOutlet weak var previewImage: UIImageView! 



override func viewDidLoad() { 
    super.viewDidLoad() 

    captionTextView.delegate = self 

    // Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


@IBAction func addImageTapped(sender: AnyObject) { 

    let imagePicker = UIImagePickerController() 

    imagePicker.delegate = self 
    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary 
    imagePicker.mediaTypes = UIImagePickerController.availableMediaTypesForSourceType(.PhotoLibrary)! 
    imagePicker.allowsEditing = false 

    self.presentViewController(imagePicker, animated: true, completion: nil) 

} 

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) { 

    self.previewImage.image = image 

    self.dismissViewControllerAnimated(true, completion: nil) 
} 

func textViewShouldEndEditing(textView: UITextView) -> Bool { 
    captionTextView.resignFirstResponder() 
    return true; 
} 

@IBAction func composeTapped(sender: AnyObject) { 

    let date = NSDate() 
    let dateFormatter = NSDateFormatter() 
    dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle 
    dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle 
    let localDate = dateFormatter.stringFromDate(date) 

    let imageToBeUploaded = self.previewImage.image 
    let imageData = UIImagePNGRepresentation(imageToBeUploaded!)! 

    let file: PFFile = PFFile(data: imageData)! 


    let fileCaption: String = self.captionTextView.text 

    let photoToUpload = PFObject(className: "Posts") 
    photoToUpload["Image"] = file 
    photoToUpload["Caption"] = fileCaption 
    photoToUpload["addedBy"] = PFUser.currentUser()?.username 
    photoToUpload["data"] = localDate 

    //Get bytes size of image 
    /* var imageSize = Float(imageData!.length) 
    //Transform into Megabytes 
    imageSize = imageSize/(1024*1024) 
    print("Image size is \(imageSize)Mb") 
    */ 


    do{ 
     try photoToUpload.save() 
    } catch _ { 

    } 

    print("Successfully Posted.") 

    let vc = self.storyboard?.instantiateViewControllerWithIdentifier("postController") 
    self.presentViewController(vc! as UIViewController, animated: true, completion: nil) 


} 
+1

您不應該打開一個可選的值而不做檢查來查看它是否爲零。你有沒有查過 –

回答

2

看來你正在解開一個可選的值而沒有真正的檢查它是否爲零。可能是let file: PFFile = PFFile(data: imageData)!。在打開變量之前,您必須檢查零。

+0

你能給我舉個例子嗎? –

相關問題