2016-05-29 66 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

相关问题