2016-05-29 65 views
1

每當我從Firebase存儲中下載圖片時,它都會完美下載,但是一旦我嘗試將imageview「我的」圖片更改爲圖片視圖,imageview消失。我沒有約束,我導航到本地URL並在那裏找到完美的圖像。任何幫助?難道我做錯了什麼?從Swift 2表格下載圖片Firebase存儲

func loadImages(){ 
     let documentDirectoryURL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true) 

      let newRef = storageRef!.child("Images/0.jpg") 
      let fileDestinationUrl = documentDirectoryURL.URLByAppendingPathComponent("p.jpg") 


      let downloadTask = newRef.writeToFile(fileDestinationUrl){ (URL, error) -> Void in 
       if (error != nil) { 
        print("problem") 
       } else { 
        print("done") 
       } 
      } 

     downloadTask.observeStatus(.Success) { (snapshot) -> Void in 
      print(fileDestinationUrl) 
      var temp = String(fileDestinationUrl) 
      print(temp) 
      var my = UIImage(contentsOfFile: temp) 
      self.image.image = my 
     } 


     } 

回答

0

打印在控制檯。去的目標網址給你下載的文件的位置,打開你的終端,其拖放到終端刪除下載的圖像和終端會給你的實際path.Now比較兩者的路徑終端給你的那個和控制檯給你的那個,匹配那些。最喜歡他們是不同的,相應地改變他們。

示例代碼: -

上傳代碼: -

func profilePictureUploading(infoOnThePicture : [String : AnyObject],completionBlock : (()->Void)) { 

    if let referenceUrl = infoOnThePicture[UIImagePickerControllerReferenceURL] { 
     print(referenceUrl) 
     let assets = PHAsset.fetchAssetsWithALAssetURLs([referenceUrl as! NSURL], options: nil) 
     print(assets) 
     let asset = assets.firstObject 
     print(asset) 
     asset?.requestContentEditingInputWithOptions(nil, completionHandler: { (ContentEditingInput, infoOfThePicture) in 

      let imageFile = ContentEditingInput?.fullSizeImageURL 
      print("imagefile : \(imageFile)") 

      let filePath = FIRAuth.auth()!.currentUser!.uid + "/\(Int(NSDate.timeIntervalSinceReferenceDate() * 1000))/\(imageFile!.lastPathComponent!)" 
      print("filePath : \(filePath)") 

       FIRControllerClass.storageRef.child("ProfilePictures").child(filePath).putFile(imageFile!, metadata: nil, completion: { (metadata, error) in 

         if error != nil{ 

          print("error in uploading image : \(error)") 

         } 

          else{ 

           print("metadata in : \(metadata!)") 

           print(metadata?.downloadURL()) 

           print("The pic has been uploaded") 

           print("download url : \(metadata?.downloadURL())") 

           self.uploadSuccess(metadata!, storagePath: filePath) 

           completionBlock() 

       } 

      }) 
     }) 

    }else{ 

      print("No reference URL found!") 

    } 
} 

下載代碼: -

let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) 
    print("paths in user home page : \(paths)") 

    let documentDirectory = paths[0] 
    print("documents directory in homePage : \(documentDirectory)") 

    let filePath = "file:\(documentDirectory)/\(user!.uid).jpg" 
    var filePath2 : String = filePath 
    print(filePath) 

    let storagePath = NSUserDefaults.standardUserDefaults().objectForKey("storagePath") as! String 
    print("storagePath is : \(storagePath)") 

    storageRef.child("ProfilePictures").child(storagePath).writeToFile(NSURL.init(string: filePath)! , completion : 

    { (url, err) -> Void in 

     if let error = err{ 

      print("error while downloading your image :\(error)") 


     }else{ 

      print("Download successful !") 
      print("the file filePath of the downloaded file : \(filePath)") 
      filePath2 = "\(documentDirectory)/\(self.user!.uid).jpg" 
      if let downloadedImage = UIImage(contentsOfFile: filePath2){ 
      self.profilePictureImageView.image = downloadedImage 
      print("displayed") 
      }else{ 
      print("unable to display") 
      } 
     } 
    }) 

其中storagePath是您存儲在NSUserDefaults的供以後參考的東西,如這一點,同時上傳您的圖片到您的Firebase存儲

我給你的codeBlocks只是衆多解決方案之一,有很多方法可以做到這一點,去https://firebase.google.com/docs/storage/ios/download-files

+0

如果它解決了你的問題,請接受它作爲答案:) – Dravidian

相關問題