从照片库中拍摄照片或选择照片后,UIImageView不会使用新图像更新 [英] UIImageView not updating with new image after picture taken or picture chosen from photo library
问题描述
拍照或从照片库中选择图片后,我试图在UIImageView中填充图像.
I'm trying to populate an image within a UIImageView after I either take a picture or choose a picture from my photo library.
以下是处理相机和照片库激活的代码:
here is the code handling the camera and photo library activations:
@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中填充图像的代码:
Here is the code that populates the image within the 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空间.
currently after I take a picture with my camera or choose an image from my photo library, I'm redirected to the initial view of the app without UIImageView space updated with the photo.
感谢您的帮助!
推荐答案
在Swift 3
中,您需要通过在keys
下方添加plist
来访问photoLibrary,并且需要使用适当的delegate
方法.
In Swift 3
you need permission to access photoLibrary by adding below keys
to your plist
and you need to use the proper delegate
method.
添加UIImagePickerControllerDelegate
& UINavigationControllerDelegate
设置为class
并将imagePicker delegate
设置为viewDidLoad
&尝试下面的代码.
Add UIImagePickerControllerDelegate
& UINavigationControllerDelegate
to your class
and set imagePicker delegate
to viewDidLoad
& Try below code.
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
这篇关于从照片库中拍摄照片或选择照片后,UIImageView不会使用新图像更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!