Reputation: 105
i want to get image from library but, image not show until I choose from library
this is my code
@IBAction func btnProfile(_ sender: UIButton) {
let alert = UIAlertController(title: "Change Profil Image", message: "Pilih Foto", preferredStyle: UIAlertControllerStyle.actionSheet)
alert.addAction(UIAlertAction(title: "Gunakan Kamera", style: .default, handler: { (action: UIAlertAction!) in
print("Gunakan Kamera")
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}))
alert.addAction(UIAlertAction(title: "Ambil Foto dari Galery", style: .default, handler: { (action: UIAlertAction!) in
print("Ambil Foto dari Galery")
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
print("Cancel")
}))
present(alert, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.originalImage] as? UIImage else { return } // error in here >> 'UIImage?' is not convertible to 'UIImage' >> and warning yellow >> Cast from 'Slice<Dictionary<UIImagePickerController.InfoKey, Any>>' (aka 'Slice<Dictionary<NSString, Any>>') to unrelated type 'UIImage' always fails
picker.dismiss(animated: true, completion: nil)
imgProf.image = image
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
I want to display the image on UIimageview, but after I retrieve it from the library it doesn't always appear, error in guard let image, and warning yellow in there i use swift 3
this is UIImagePickerControllerDelegate
public protocol UIImagePickerControllerDelegate : NSObjectProtocol {
@available(iOS 2.0, *)
optional public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
@available(iOS 2.0, *)
optional public func imagePickerControllerDidCancel(_ picker: UIImagePickerController)
}
Upvotes: 0
Views: 903
Reputation: 36
self.present(imagePicker, animated: true, completion: {
imagePicker.delegate = self
})
Set picker delegate in completion.
Upvotes: 0
Reputation: 105
I tried this code and it worked
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selectedImageFromPicker: UIImage?
if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
selectedImageFromPicker = editedImage
} else if let originalImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
selectedImageFromPicker = originalImage
}
if let selectedImage = selectedImageFromPicker {
imgProf.image = selectedImage
}
dismiss(animated: true, completion: nil)
}
and thanks for @Kamran, your answer for change the position is true
Upvotes: 1