1

每当我从 Firebase Storage 下载图像时,它的下载效果都很完美,但是一旦我尝试将图像视图“我的”更改为它,图像视图就会消失。我没有限制,我导航到本地 URL 并在那里完美地找到了图像。有什么帮助吗?难道我做错了什么?

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
        }


       }
4

1 回答 1

0

在您的控制台中打印destinationUrl。转到您下载的文件位置,打开您的终端,将下载的图像拖放到终端中,终端将为您提供其实际路径。现在比较终端给您的路径和一个控制台给你的,匹配那些。最喜欢它们是不同的,相应地改变它们。

示例代码:-

上传代码: -

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 存储时

我给你的代码块只是众多解决方案之一,有很多方法可以做到这一点,请访问https://firebase.google.com/docs/storage/ios/download-files

于 2016-07-06T15:47:53.030 回答