2016-08-14 19 views
0

我有一個按鈕來拍攝圖像。我希望將從相機拍攝的圖像傳遞給另一個視圖控制器。第一個視圖控制器的代碼如下所示。無法將從相機拍攝的圖像傳遞到另一個視圖控制器

@IBAction func takePhoto(sender: AnyObject) 
{ let picker = UIImagePickerController() 
    picker.delegate = self 
    picker.sourceType = .Camera 
    presentViewController(picker, animated: true,completion : nil) 

} 


func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) 
{ 
    TakenImage = info[UIImagePickerControllerOriginalImage] as? UIImage ; dismissViewControllerAnimated(true, completion: nil) 

let controller = self.storyboard!.instantiateViewControllerWithIdentifier("NoteDetailViewController") as! NoteDetailViewController 

    controller.takinPhoto = true 

    if (note != "") 
    { 
     controller.content = note 
    } 

    controller.imageFromCamera = TakenImage 

    if (self.tags != "") 
    { 
     controller.tagsTextField.text = self.tags 
    } 
    self.presentViewController(controller, animated: true, completion: nil) 
} 
@IBAction func save(sender: AnyObject) 
{ 
    UIImageWriteToSavedPhotosAlbum(TakenImage!, self, "image:didFinishSavingWithError:contextInfo:", nil) 
} 
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) 
{ 
    if error == nil { 
     let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .Alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
     presentViewController(ac, animated: true, completion: nil) 
    } else 
    { 
     let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
     presentViewController(ac, animated: true, completion: nil) 
    } 
} 

用於我的第二視圖控制器的代碼如下所示

if (takinPhoto == true) 
    { 
     if (imageFromCamera != nil) 
     { 
      if let image1 = self.imageFromCamera 
      { 
       self.imageView2.image = image1 
      } 

     } 
     if (self.content != "") 
     { 
      self.contentTextField2.text = content 
     } 

    } 

但是,從攝像機圖像未在第二viewcontroller.How我可以解決這個出現?

+0

您使用哪種方法顯示傳遞的照片? –

+0

我有代碼來顯示圖像viewWillAppear方法的第二個視圖控制器 –

回答

0

已更新的答案。

我重寫了你的代碼,並能夠在本地工作。主要變化是使用特定於照片的不同代表方法。

@IBAction func takePhoto(sender: AnyObject) { let picker = UIImagePickerController() 
    picker.delegate = self 
    picker.sourceType = .Camera 
    presentViewController(picker, animated: true,completion : nil) 
} 

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { 
    takenImage = image 
    dismissViewControllerAnimated(true, completion: nil) 
} 

@IBAction func save(sender: AnyObject) { 
    UIImageWriteToSavedPhotosAlbum(takenImage!, self, #selector(ViewController.image(_:didFinishSavingWithError:contextInfo:)), nil) 
} 

func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) { 
    if error == nil { 
     let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .Alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
     presentViewController(ac, animated: true, completion: nil) 
    } else { 
     let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert) 
     ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
     presentViewController(ac, animated: true, completion: nil) 
    } 
} 

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if (segue.identifier == "saveTakenImage") { 
     let controller = segue.destinationViewController as! NoteDetailViewController 
     controller.takinPhoto = true 
     if (note != "") { 
      controller.content = note 
     } 

     controller.imageFromCamera = takenImage 

     if (self.tags != "") { 
      controller.tagsTextField.text = self.tags 
     } 
    } 
} 
+0

它在第一個視圖控制器中聲明爲 var TakenImage:UIImage? –

+0

我該如何解決這個錯誤 –

+0

更新了我的答案並修改了使用camelCase的變量。 – CodeBender

相關問題