如何将UIImage发送到Firestore?

时间:2019-04-03 23:08:05

标签: ios swift google-cloud-firestore

是否可以在不将此图像保存到照片的情况下发送到我的Firestore数据库?我知道Firestore希望如何引用它,唯一的方法是从照片中选择一张照片。使用我的应用程序,我觉得向他们的照片寻求许可是多余的,因为我只想直接将拍摄的照片发送到数据库中。

1 个答案:

答案 0 :(得分:0)

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

下面我将为您提供一个代码示例:

    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

                })
            }
     }