[英]UIImageview not updating once camera picture is taken but when gallery image is selected, camera picture shows for a second
[英]UIImageView not updating with new image after picture taken or picture chosen from photo library
拍照或从照片库中选择图片后,我试图在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空间。
任何帮助表示赞赏,谢谢!
在Swift 3
您需要通过在plist
添加以下keys
来访问photoLibrary的权限,并且需要使用适当的delegate
方法。
将UIImagePickerControllerDelegate
和UINavigationControllerDelegate
添加到您的class
,并将imagePicker delegate
设置为viewDidLoad
并尝试以下代码。
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
这似乎是与XCode相关的问题,未能解释imagePickerController函数语法已更改(自Swift 3起)。 您将在网上找到这两种编写函数的方式。 您还可以在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]) { }
**您需要像全局对象一样保存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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.