Ios 如何将拍摄/拾取的图像设置为uiimage?

Ios 如何将拍摄/拾取的图像设置为uiimage?,ios,swift,uiimageview,uiimage,Ios,Swift,Uiimageview,Uiimage,我成功拍摄或拾取照片并上传到Firebase存储,但我不确定如何将该照片设置为UIImage,请参阅代码: 需要将照片设置为的UIImage: @IBOutlet weak var myPhoto: UIImageView! 如何选择或拍照: imagePicker.allowsEditing = true let alertController = UIAlertController(title: "Add a Photo", message: "Choose From", pre

我成功拍摄或拾取照片并上传到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(picker)实例的委托


 请求许可

在访问应用程序之前,应用程序必须获得用户的许可 照相机/已保存的照片。应用程序应向用户显示一条消息 解释为什么需要摄像头或照片库访问。你可以 通过设置nsCamerauseControlation和 应用程序的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相关的所有其他内容 官方参考链接


将图像选取器的委托设置为
self
,并实现
imagePickerController(uxFinishPickingMediaWithInfo:)