2016-07-11 55 views
0

我看了很多解決方案,可悲的是,沒有爲我工作。我用這個代碼來創建一個imagePickerController:UIImageView沒有顯示來自imagePickerController的圖片

@IBAction func takePhoto(sender: UIButton) { 
    imagePicker = UIImagePickerController() 
    imagePicker.delegate = self 
    imagePicker.sourceType = .Camera 
    imagePicker.mediaTypes = [kUTTypeImage as String] 
    imagePicker.allowsEditing = false 

    presentViewController(imagePicker, animated: true, completion: nil) 
} 

func imagePickerController(picker: UIImagePickerController, _didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
     let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage 
     imageView.contentMode = .ScaleAspectFit 
     imageView.image = pickedImage 
     dismissViewControllerAnimated(true, completion: nil) 
} 

但仍然我在相機上的圖像不顯示在imageView中。我還導入了正確的代表。當我嘗試將imageView.image打印到控制檯時,它會返回nil。我究竟做錯了什麼?

+0

嘗試'FUNC imagePickerController(_選擇器:的UIImagePickerController,didFinishPickingMediaWithInfo信息:[字符串:AnyObject]){' –

+1

作品!下劃線爲了使其工作而做了什麼? –

+0

您的方法聲明需要與picker委託方法完全相同,否則將永遠不會被調用。您應該始終讓Xcode爲您自動完成它。 –

回答

1

請嘗試此代碼。希望它能幫助你。

func cameraPhoto(){ 

     imagePicker.allowsEditing = true 
     imagePicker.delegate = self 
     imagePicker.sourceType = UIImagePickerControllerSourceType.Camera 
     imagePicker.cameraCaptureMode = .Photo 
     imagePicker.cameraDevice = .Front; 
     presentViewController(imagePicker, animated: true, completion: nil) 

} 

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 


    let chosenImage = info[UIImagePickerControllerEditedImage] as! UIImage 
} 

感謝