2016-08-14 3 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) 
    } 
} 

私の第二のViewControllerのコードは

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

私は、第2のviewcontrollerの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

最初のviewcontrollerには と宣言されていますvar TakenImage:UIImage? –

+0

このエラーを解決するにはどうすればいいですか? –

+0

私の回答が更新され、camelCaseも使用するように変数が変更されました。 – CodeBender

関連する問題