9

我有一个UIImagePicker设置在我的应用程序,工作正常。我想在我的UIImage选择器被选中时上传个人资料图片到Firebase。这是我选择照片时的功能。从UIImage Picker上传图片到新的Firebase(Swift)

//image picker did finish code 
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 

    let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage 
    profilePic.contentMode = .ScaleAspectFill 
    profilePic.image = chosenImage 
    profilePic.hidden = false 
    buttonStack.hidden = true 
    changeButtonView.hidden = false 
    self.statusLabel.text = "Here is Your Profile Picture" 

    dismissViewControllerAnimated(true, completion: nil) 


} 

新文档声明我们需要声明NSURl以上传文件。这里是我试图找到给定文件的NSURL,但它不起作用。这里是文档和一个链接:https://firebase.google.com/docs/storage/ios/upload-files#upload_from_data_in_memory

// File located on disk 
let localFile: NSURL = ... 
// Create a reference to the file you want to upload 
let riversRef = storageRef.child("images/rivers.jpg") 

// Upload the file to the path "images/rivers.jpg" 
let uploadTask = riversRef.putFile(localFile, metadata: nil) { metadata, error in 
    if (error != nil) { 
    // Uh-oh, an error occurred! 
    } else { 
    // Metadata contains file metadata such as size, content-type, and download URL. 
    let downloadURL = metadata!.downloadURL 
    } 
} 

这里是我的检索UIImagePicker的NSURL尝试:

//image picker did finish code 
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 

     let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage 
     //getting the object's url 
     let imageUrl = info[UIImagePickerControllerReferenceURL] as! NSURL 
     let imageName = imageUrl.lastPathComponent 
     let documentDir = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! as String; 
     let photoUrl = NSURL(fileURLWithPath: documentDir) 
     let localPath = photoUrl.URLByAppendingPathComponent(imageName!) 
     self.localFile = localPath 

     profilePic.contentMode = .ScaleAspectFill 
     profilePic.image = chosenImage 
     profilePic.hidden = false 
     buttonStack.hidden = true 
     changeButtonView.hidden = false 
     self.statusLabel.text = "Here is Your Profile Picture" 

     dismissViewControllerAnimated(true, completion: nil) 


    } 

我相信,我也遇到了困难,如果图像是因为它尚未保存在设备上,而不是从相册中取出。我如何找到这张图片/快照的NSURL?

回答

24

这里是我的方法来上传和下载从火力存储用户的个人资料照片:

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { 
    userPhoto.image = image 
    dismissViewControllerAnimated(true, completion: nil) 
    var data = NSData() 
    data = UIImageJPEGRepresentation(userPhoto.image!, 0.8)! 
    // set upload path 
    let filePath = "\(FIRAuth.auth()!.currentUser!.uid)/\("userPhoto")" 
    let metaData = FIRStorageMetadata() 
    metaData.contentType = "image/jpg" 
    self.storageRef.child(filePath).putData(data, metadata: metaData){(metaData,error) in 
     if let error = error { 
      print(error.localizedDescription) 
      return 
     }else{ 
     //store downloadURL 
     let downloadURL = metaData!.downloadURL()!.absoluteString 
     //store downloadURL at database 
    self.databaseRef.child("users").child(FIRAuth.auth()!.currentUser!.uid).updateChildValues(["userPhoto": downloadURL]) 
     } 

     } 
        } 

我还存储了图像的URL到火力数据库,并检查用户是否拥有个人资料照片或u可能会崩溃:

//get photo back 

    databaseRef.child("users").child(userID!).observeSingleEventOfType(.Value, withBlock: { (snapshot) in 
      // check if user has photo 
      if snapshot.hasChild("userPhoto"){ 
       // set image locatin 
       let filePath = "\(userID!)/\("userPhoto")" 
       // Assuming a < 10MB file, though you can change that 
       self.storageRef.child(filePath).dataWithMaxSize(10*1024*1024, completion: { (data, error) in 

        let userPhoto = UIImage(data: data!) 
        self.userPhoto.image = userPhoto 
       }) 
      } 
     }) 
+0

你也可以创建这样的路径:let path =“\(NSUUID()。uuidString)” – Neen

+0

@Neen伟大的建议!谢谢。 –

+0

上传位的效果很好。下载不适合我。我必须以不同的方式做。 –

相关问题