我成功拍摄或挑选照片并上传到Firebase存储但我不知道如何将该照片设置为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)
如何设置刚刚选择或拍摄到myPhoto的照片?
您可以从UIImagePickerControllerDelegate函数中获取选定或捕获的图像。您必须设置UIImagePickerController(选择器)实例的委托。
要求许可
在访问相机/保存的照片之前,应用程序必须征得用户的许可。应用程序应向用户显示一条消息,说明为何需要相机或照片库访问权限。您可以通过在应用的Info.plist文件中设置NSCameraUsageDescription和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相关的所有其他内容。