无法使用iOS Swift API将图像上传到Google Firebase存储

时间:2018-08-24 06:53:40

标签: ios swift firebase image-uploading firebase-storage

之前,我能够将图像上传到Google Firebase Strage。但突然间它不起作用。

下面是代码:

private func uploadEmergencyImages(images: [Int: URL], emergencyId: String){
    let imagesRef = Storage.storage().reference().child("incidents").child(emergencyId).child("images")

    if(!images.isEmpty){
        for (key, value) in images {
            print("Image to be Uploaded = \(value.absoluteString)")

            let imageData = try! Data(contentsOf: value)
            //                let image = UIImage(data: imageData)

            let metaData = StorageMetadata()
            metaData.contentType = "image/png"
            imagesRef.child("Image_\(key+1).png").putData(imageData, metadata: metaData) { (storageMetaData, storageError) in
                if(storageError != nil){
                    print("Error - While uploading Image:: \(storageError.debugDescription)")
                }else{
                    print("Upload completed...")
                }
            }
        }
    }
}

下面是错误消息(注意:在此,我已用“ XXX”遮盖了我的firebasePrjName。)

  

2018-08-23 20:25:54.758092 + 0300 XXX [44855:1058133] [AXRun-PID]客户端   请求中止PID:44870名称:2018-08-23   20:25:55.833590 + 0300 XXX [44855:1057851] [AXRun-PID]客户端请求   不中止PID:44870名称:2018-08-23   20:25:57.512286 + 0300 XXX [44855:1057987] [发现]遇到错误   发现扩展名时:Error Domain = PlugInKit Code = 13“ query   已取消” UserInfo = {NSLocalizedDescription =查询已取消}新增   事件键= -LKboaL9nTohkCHuYp1U要上传的图片=   文件:/// Users / macbookpro / Library / Developer / CoreSimulator / Devices / 863EEA6A-F1F5-4A6C-B19B-D686D81486F4 / data / Containers / Data / Application / F0D6F426-EE5E-4B45-8D1F-3B880C049DF3 / tmp / 39F05E90-3 -4A40-84CB-1931D1E9C7CA.jpeg   2018-08-23 20:26:07.359567 + 0300 XXX [44855:1058132] TI​​C TCP连接失败   [4:0x60400036ffc0]:3:-9802 Err(-9802)2018-08-23 20:26:07.360138 + 0300   XXX [44855:1058132] NSURLSession / NSURLConnection HTTP加载失败   (kCFStreamErrorDomainSSL,-9802)2018-08-23 20:26:07.360235 + 0300   XXX [44855:1058132]任务。<1>   HTTP加载失败(错误代码:-1200 [3:-9802])2018-08-23   20:26:07.360437 + 0300 XXX [44855:1057735]任务   。<1>完成,错误-代码:   -1200错误-上传图像时::可选(错误域= FIRStorageErrorDomain代码= -13000“发生未知错误,   请检查服务器响应。”   UserInfo = {NSLocalizedRecoverySuggestion =您要连接到   还是服务器?,NSLocalizedDescription =发生未知错误,   请检查服务器响应。_kCFStreamErrorDomainKey= 3,   NSErrorPeerCertificateChainKey =(       “”,       “”),NSErrorClientCertificateStateKey = 0,   NSErrorFailingURLKey = https://firebasestorage.googleapis.com/v0/b/XXX-ots.appspot.com/o/incidents%2F-LKboaL9nTohkCHuYp1U%2Fimages%2FImage_1.png?uploadType=resumable&name=incidents%2F-LKboaL9nTohkCHuYp1U%2Fimages%2FImage_1.png,   NSErrorFailingURLStringKey = https://firebasestorage.googleapis.com/v0/b/XXX-ots.appspot.com/o/incidents%2F-LKboaL9nTohkCHuYp1U%2Fimages%2FImage_1.png?uploadType=resumable&name=incidents%2F-LKboaL9nTohkCHuYp1U%2Fimages%2FImage_1.png,   NSUnderlyingError = 0x600000647290 {Error Domain = kCFErrorDomainCFNetwork   代码= -1200“(空)”   UserInfo = {_ kCFStreamPropertySSLClientCertificateState = 0,   kCFStreamPropertySSLPeerTrust =,   _kCFNetworkCFStreamSSLErrorOriginalValue = -9802,_kCFStreamErrorDomainKey = 3,_kCFStreamErrorCodeKey = -9802,kCFStreamPropertySSLPeerCertificates =(       “”,       “”}}},_ kCFStreamErrorCodeKey = -9802,   object = incidents / -LKboaL9nTohkCHuYp1U / images / Image_1.png,   ResponseErrorDomain = NSURLErrorDomain,bucket = XXX-ots.appspot.com,   NSURLErrorFailingURLPeerTrustErrorKey =,   ResponseErrorCode = -1200})

我也尝试过如下修改我的info.plist文件,但是没有用:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

任何潜在客户都受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

这对我有用(swift 4代码)

let storageRef = DBProvider.Instance.imageStorageRef.child(user!.uid) // Put Here your Storage Referance  
let imageData = UIImageJPEGRepresentation(youimagehere, 1.0) // put your image Here

storageRef.putData(imageData!, metadata: nil, completion: { (metadata, error) in

    if error == nil
    {
        let path = metadata?.downloadURL()?.absoluteString
        print(path)
    }
})

确保已启用Firebase存储从Firebase项目读取,写入权限。

enter image description here