2016-05-29 8 views
1

Firebase Storageからイメージをダウンロードするたびにダウンロードは完璧ですが、一度イメージビュー "my"を変更しようとするとイメージビューが消えます。私には制約がなく、ローカルのURLに移動して、そこに完全にイメージがあることがわかりました。どんな助け?私は何か間違っているのですか?Swift 2形式でイメージをダウンロードするFirebase Storage

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

ダウンロードしたファイルの場所への印刷コンソール.GOでdestinationUrlに、あなたのターミナルを開き、ドラッグし、端末にダウンロードした画像をドロップすると、端末は、パスの両方を比較し、あなたにその実際の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のストレージに画像をアップロード中に

私があなたに与えたコードブロックは、多くの解決策の1つにすぎません。これにはたくさんの方法があります。https://firebase.google.com/docs/storage/ios/download-files

+0

問題を解決したら、これを回答として受け入れてください:) – Dravidian

関連する問題