func imagePickerController()

in storage/StorageExampleSwift/ViewController.swift [67:120]


  func imagePickerController(_ picker: UIImagePickerController,
                             didFinishPickingMediaWithInfo info: [
                               UIImagePickerController.InfoKey: Any
                             ]) {
    // Local variable inserted by Swift 4.2 migrator.
    let info = convertFromUIImagePickerControllerInfoKeyDictionary(info)

    picker.dismiss(animated: true, completion: nil)

    urlTextView.text = "Beginning Upload"
    // if it's a photo from the library, not an image from the camera
    if let referenceUrl =
      info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey
          .referenceURL)] as? URL {
      let assets = PHAsset.fetchAssets(withALAssetURLs: [referenceUrl], options: nil)
      let asset = assets.firstObject
      asset?.requestContentEditingInput(with: nil, completionHandler: { contentEditingInput, info in
        let imageFile = contentEditingInput?.fullSizeImageURL
        let filePath = Auth.auth().currentUser!.uid +
          "/\(Int(Date.timeIntervalSinceReferenceDate * 1000))/\(imageFile!.lastPathComponent)"
        // [START uploadimage]
        let storageRef = self.storage.reference(withPath: filePath)
        storageRef.putFile(from: imageFile!) { result in
          switch result {
          case .success:
            self.uploadSuccess(storageRef, storagePath: filePath)
          case let .failure(error):
            print("Error uploading: \(error)")
            self.urlTextView.text = "Upload Failed"
          }
        }
        // [END uploadimage]
      })
    } else {
      guard let image =
        info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey
            .originalImage)] as? UIImage else { return }
      guard let imageData = image.jpegData(compressionQuality: 0.8) else { return }
      let imagePath = Auth.auth().currentUser!.uid +
        "/\(Int(Date.timeIntervalSinceReferenceDate * 1000)).jpg"
      let metadata = StorageMetadata()
      metadata.contentType = "image/jpeg"
      let storageRef = storage.reference(withPath: imagePath)
      storageRef.putData(imageData, metadata: metadata) { result in
        switch result {
        case .success:
          self.uploadSuccess(storageRef, storagePath: imagePath)
        case let .failure(error):
          print("Error uploading: \(error)")
          self.urlTextView.text = "Upload Failed"
        }
      }
    }
  }