我可以成功地拍摄或挑选照片并将其上传到Firebase Storage,但是我不确定如何将该图片设置为UIImage,请参见代码:
需要将照片设置为的UIImage
@IBOutlet weak var myPhoto: UIImageView!
如何选择或拍照:
imagePicker.allowsEditing = true
let alertController = UIAlertController(title: "Add a Photo", message: "Choose From", preferredStyle: .actionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: .default) { (action) in
self.imagePicker.sourceType = .camera
self.imagePicked = sender.tag // new
self.present(self.imagePicker, animated: true, completion: nil)
}
let photosLibraryAction = UIAlertAction(title: "Photos Library", style: .default) { (action) in
self.imagePicker.sourceType = .photoLibrary
self.imagePicked = sender.tag // new
self.present(self.imagePicker, animated: true, completion: nil)
}
let savedPhotosAction = UIAlertAction(title: "Saved Photos Album", style: .default) { (action) in
self.imagePicker.sourceType = .savedPhotosAlbum
self.imagePicked = sender.tag // new
self.present(self.imagePicker, animated: true, completion: nil)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)
alertController.addAction(cameraAction)
alertController.addAction(photosLibraryAction)
alertController.addAction(savedPhotosAction)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
如何将刚刚选择或拍摄的照片设置到我的照片中?
答案 0 :(得分:0)
您可以从中选择或捕获图像 UIImagePickerControllerDelegate函数。您必须设置 您的UIImagePickerController(picker)实例的委托。
寻求许可
该应用必须先获得用户的许可,然后才能访问 相机/已保存的照片。应用应向用户显示一条消息 解释为什么需要相机或照片库访问权限。您可以 通过设置NSCameraUsageDescription设置此消息,然后 应用程序的Info.plist文件中的NSPhotoLibraryUsageDescription键。
这肯定会起作用
imagePicker.delegate = self
extension StackOverflowViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController( picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
var selectedImage: UIImage?
if let editedImage = info[.editedImage] as? UIImage {
selectedImage = editedImage
self.imgView.image = selectedImage!
} else if let originalImage = info[.originalImage] as? UIImage {
selectedImage = originalImage
self.imgView.image = selectedImage!
picker.dismiss(animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel( picker: UIImagePickerController) {
picker.dismiss(animated: true) {
// Further logic to perform
}
}
}
您可以从此处查看与UIImagePickerController相关的所有其他内容 官方参考链接。