2016-11-27 39 views
0

我試圖在UIImageView中填充圖像後,我拍照或從我的照片庫中選擇一張圖片。UIImageView沒有更新與圖片拍攝後的新圖像或從照片庫中選擇的圖片

這裏是處理相機和照片庫激活代碼:

@IBAction func activateCmera(_ sender: Any) { 
    if (UIImagePickerController.isSourceTypeAvailable(.camera)) { 
     let imagePicker = UIImagePickerController() 
     imagePicker.delegate = self 
     imagePicker.sourceType = .camera 
     imagePicker.allowsEditing = false 
     self.present(imagePicker, animated: true, completion: nil) 
    } 
} 


@IBAction func activatePhotoLib(_ sender: Any) { 
    if (UIImagePickerController.isSourceTypeAvailable(.photoLibrary)) { 
     let imagePicker = UIImagePickerController() 
     imagePicker.delegate = self 
     imagePicker.sourceType = .photoLibrary 
     imagePicker.allowsEditing = false 
     self.present(imagePicker, animated: true, completion: nil) 
    } 
} 

這裏是填充的UIImageView中的圖像的代碼:我有一張照片後

@IBOutlet weak var imageView: UIImageView! 

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage 
    self.imageView.contentMode = .scaleAspectFit 
    self.imageView.image = image 
    self.dismiss(animated: true, completion: nil); 
} 
目前

我相機或從我的照片庫中選擇圖像,我將重定向到應用程序的初始視圖,而不更新UIImageView空間的照片。

任何幫助表示讚賞謝謝!

回答

3

Swift 3您需要訪問photoLibrary的權限,方法是將keys添加到您的plist,並且您需要使用正確的delegate方法。

enter image description here

添加UIImagePickerControllerDelegate & UINavigationControllerDelegateclass並設置imagePicker delegateviewDidLoad &試試下面的代碼。

let imagePicker = UIImagePickerController() 

    @IBAction func activatePhotoLib(_ sender: UIButton) { 
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) { 
     imagePicker.delegate = self 
     imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary; 
     imagePicker.allowsEditing = false 
     self.present(imagePicker, animated: true, completion: nil) 
     } 
    } 

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { 
     imageView.image = image 
    } 
    picker.dismiss(animated: true, completion: nil); 
    } 

輸出:updated

enter image description here

+0

感謝那些工作! – Jmuru

0

這似乎是無法解釋的是,imagePickerController函數的語法已經改變(因爲斯威夫特3)涉及XCode的一個問題。你會發現這兩種在網絡上編寫函數的方法。您還可以在XCode中使用這兩種方法而不會出現任何錯誤。但只有第二個才能正確地獲得圖像。

(Option 1 - doesn't work, but XCode doesn't flag it): 
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { } 

(Option 2 - this is the right syntax!): 
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { } 
0

**您需要保存的UIImagePickerController像全局對象,在這之後,你需要在你的行動功能,可按下此代碼 **

@objc func getImage() { 

    imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary 

    imagePicker.allowsEditing = false 

    imagePicker.modalPresentationStyle = .overCurrentContext 

    present(imagePicker, animated: true) 

} 

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

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { 
    imageView.image = image 
} 

self.dismiss(animated: true, completion: nil); 

} 
相關問題