从 swift 中的图库中选择照片不起作用
Choose photo from library in swift doesn't work
在我的应用程序中,我有一个 "addImgBtn" 应该让用户从他的照片中选择图像。我还有 "profileImg" 存储一些临时图像。
当我点击 "addImg" 按钮时,它会显示所有照片,但是当我点击 "Choose" 时,它不会将临时图片替换为所选图片,这里是我的代码:
class myClass: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@IBOutlet weak var profileImg: UIImageView!
@IBOutlet weak var addImgBtn: UIButton!
@IBAction func addImgBtn(_ sender: Any) {
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismiss(animated: true, completion: { () -> Void in
})
profileImg.image = image
}
}
你使用的imagePickerController方法有误,改成如下:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
self.dismiss(animated: true, completion: { () -> Void in
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
self.profileImg.image = image
}
})
}
这应该有效!作为一般提示,命令单击 UIImagePickerControllerDelegate
或您在 xcode 中实现的任何委托,它会告诉您所有可用的方法等
在我的应用程序中,我有一个 "addImgBtn" 应该让用户从他的照片中选择图像。我还有 "profileImg" 存储一些临时图像。
当我点击 "addImg" 按钮时,它会显示所有照片,但是当我点击 "Choose" 时,它不会将临时图片替换为所选图片,这里是我的代码:
class myClass: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@IBOutlet weak var profileImg: UIImageView!
@IBOutlet weak var addImgBtn: UIButton!
@IBAction func addImgBtn(_ sender: Any) {
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
self.dismiss(animated: true, completion: { () -> Void in
})
profileImg.image = image
}
}
你使用的imagePickerController方法有误,改成如下:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
self.dismiss(animated: true, completion: { () -> Void in
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
self.profileImg.image = image
}
})
}
这应该有效!作为一般提示,命令单击 UIImagePickerControllerDelegate
或您在 xcode 中实现的任何委托,它会告诉您所有可用的方法等