【问题标题】:How can I send UIImage to Firestore?如何将 UIImage 发送到 Firestore?
【发布时间】:2019-04-03 23:08:04
【问题描述】:

是否可以在不将此图像保存到照片的情况下发送到我的 Firestore 数据库?我看到 Firestore 想要引用它,唯一的方法是从照片中挑选一张照片。在我的应用中,我觉得为他们的照片请求许可是多余的,因为我只想发送一张直接进入数据库的照片。

【问题讨论】:

  • UIImage 对象保存到 Firestore 云?您是否阅读过它的文档以了解您可以使用它存储哪些类型的数据类型?
  • 如果你想存储 UIImage 对象,你应该使用 Firestore 存储。

标签: ios swift google-cloud-firestore


【解决方案1】:

如果我正确理解您的问题,您绝对可以将从相机拍摄的照片直接保存到您的数据库中,而无需请求照片库访问权限。

下面我给你一个代码示例:

    func photoOutput(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {

        let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer!, previewPhotoSampleBuffer: previewPhotoSampleBuffer!)

        //set the picture taken as UIImage just in case you'd like to preview the image before saving it
        //let image = UIImage(data: imageData!)

        let storageRef = Storage.storage().reference().child("\(yourPictureName).jpg")
            guard let data = imageData else { return }
            storageRef.putData(data, metadata: nil){ (metadata, error) in
                guard metadata != nil else {
                    return
                }
                storageRef.downloadURL(completion: { (url, error) in
                    guard let downloadURL = url?.absoluteString else { return }

                    // save this URL to your database as usual

                })
            }
     }

【讨论】:

  • 为什么要将imageData 转换为UIImage,然后再将UIImage 转换回Data?只需发送imageData
  • guard let data = image.jpeg else { return } 我得到错误:“UIImage 类型的值?”没有成员“jpeg”
  • @JohnCena 查看 UIImage 的参考文档,您将看到获取 JPEG 数据的函数的正确名称。
  • @JohnCena 改用这个guard let data = image?.jpegData(compressionQuality: 0.75) else { return }
猜你喜欢
  • 2021-07-10
  • 2015-04-02
  • 1970-01-01
  • 2020-09-25
  • 2020-05-17
  • 2020-02-26
  • 2021-04-22
  • 1970-01-01
  • 2019-01-13
相关资源
最近更新 更多