我在我的应用程序中设置了一个UIImagePicker,它工作正常。当我的UIImagePicker被选中时,我想上传一张个人资料图片到Firebase。下面是我在选择图片时的函数。
//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?
3条答案
按热度按时间uyto3xhc1#
下面是我从firebase存储中上传和下载用户个人资料照片的方法:
我还将图像URL存储到firebase数据库中,并检查用户是否有个人资料照片,否则可能会导致崩溃:
3zwjbxry2#
简单的2020年例子
1.别忘了将
pod 'Firebase/Storage'
添加到您的podfile中,并且pod install
1.将两个代表添加到您的类中
1.情节提要、按钮、链接到...
然后再
但你把图像放在哪里?
在代码处,
几乎不可避免地,
·使用用户ID、聊天ID、订阅源ID或类似概念作为文件夹名称
·对于名称,唯一可能的是uuid
因此几乎总是
pgpifvop3#
在Swift 4.2中工作
在这里,我做点击图像,我已经添加了tapGesture,然后它打开画廊,然后选择图像,上传在Firebase,也我添加textField值太我希望它能帮助你,谢谢