如何通过imagePicker将GIF图像上传到Firebase?

时间:2020-08-28 03:43:10

标签: swift firebase firebase-storage

我想允许我的应用程序的用户上传GIF。

我正在使用info[UIImagePickerController.InfoKey.referenceURL] as! NSURL来获取所选GIF的临时位置。

当我尝试使用此URL信息上传到Firebase时出现此错误:

error uploading to firebase
File at URL: assets-library://asset/asset.GIF?id=6140CE69-7678-4668-BC55-EB6CF6D975CF&ext=GIF is not reachable. Ensure file URL is not a directory, symbolic link, or invalid url.

这是我尝试上传的代码段:

let imageName = UUID().uuidString
let storage = Storage.storage()
if (imagePath.hasSuffix("GIF")) {
    let storingimage = storage.reference().child("\(imageName).gif")

    let metadata = StorageMetadata()
    metadata.contentType = "image/gif"


    storingimage.putFile(from: URL(string: imagePath)!, metadata: metadata)

我之所以使用这种方法,是因为swift不能为gif图像提供jpegData()pngData()等效项。

任何人都可以分享如何解决此错误并将GIF图片保存到Firebase吗?

我也尝试过使用putData()失败

let imageData = try! Data(contentsOf: URL(string: imagePath)!)
storingimage.putData(imageData, metadata: metadata)

Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=256 "The file “asset.GIF” couldn’t be opened." UserInfo={NSURL=assets-library://asset/asset.GIF?id=6140CE69-7678-4668-BC55-EB6CF6D975CF&ext=GIF}

fileURLWithPath

let imageData = try! Data(contentsOf: URL(fileURLWithPath: imagePath))
storingimage.putData(imageData, metadata: metadata)

Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=260 "The file “asset.GIF?id=6140CE69-7678-4668-BC55-EB6CF6D975CF&ext=GIF” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/assets-library:/asset/asset.GIF?id=6140CE69-7678-4668-BC55-EB6CF6D975CF&ext=GIF, 

0 个答案:

没有答案