2015-12-11 73 views
0

我已經添加了一個相機&照片庫到我的應用程序,但我想要做的是當有人選擇相機,拍照並選擇它時,我希望它將用戶引導到新的視圖控制器並顯示那裏的圖像,而不是在按鈕所在的當前視圖上?如何在選擇/拍攝後將圖像從相機/相機膠捲傳遞到新視圖?

我在Google上搜索過,但沒有找到任何清晰或迅速的東西,因爲我對ObjC沒有任何瞭解,所以我不確定我所看到的是否正確!

這裏是下面我當前的代碼:

override func viewDidLoad() { 
    super.viewDidLoad() 

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

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


@IBAction func Camera(sender: AnyObject) { 

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) { 
    let imagePicker = UIImagePickerController() 
    imagePicker.delegate = self 
    imagePicker.sourceType = UIImagePickerControllerSourceType.Camera; 
    imagePicker.allowsEditing = false 
    self.presentViewController(imagePicker, animated: true, completion: nil) 
    } 
} 


@IBAction func Images(sender: AnyObject) { 

    let imageLoad = UIImagePickerController() 
    imageLoad.delegate = self 
    imageLoad.sourceType = UIImagePickerControllerSourceType.PhotoLibrary 
    imageLoad.allowsEditing = false 


    self.presentViewController(imageLoad, animated: true, completion: nil) 
} 


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

    self.dismissViewControllerAnimated(true, completion: nil) 

}

回答

1

基於以上的答案,你要設置圖像的圖像視圖,當你應試圖將圖像設置爲圖像視角的圖像,因此,它應該是:

func viewDidLoad() { 
    super.viewDidLoad() 

    // Because the self.image is an optional var, you need to unwrap it 
    if let image = self.image { 
     self.imageView.image = self.image 
    } 
} 
+0

非常感謝josh! – Charles

1

你只需要像分配給新的視圖控制器:

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

    self.dismissViewControllerAnimated(true, completion: nil) 

    let svc = self.storyboard!.instantiateViewControllerWithIdentifier("imageViewer") as! ImageViewController 
    svc.image = image 

    self.presentViewController(svc, animated: true, completion: nil) 
} 

,然後在imageViewerController:

func viewDidLoad() { 
    super.viewDidLoad() 

    // Because the self.image is an optional var, you need to unwrap it 
    if let image = self.image { 
     self.imageView.image = self.image 
    } 
} 
+0

喜非常感謝您的回答。我已經完成了你所說的,但我在「imageViewerController」中得到這個錯誤不能指定'UIImage'類型的值?鍵入'UIImageView!'當我添加self.imageView = self.image(我得到這個圖像錯誤)你知道爲什麼嗎? – Charles

+0

在imageViewerController中,我也有圖像視圖插座。然後一個名爲「圖像」的變種。 (var Image:UIImage?= nil) – Charles

+1

哦,忘了圖像需要是可選值,編輯我的答案。 – Fantini

相關問題