为什么Xcode会向我抱怨有关将图像上传到Firebase存储的权限的任何原因,但是当我在模拟器中运行相同的应用程序时,它可以正常工作吗?
权限错误:
无法访问正文文件:/var/mobile/Media/DCIM/100APPLE/IMG_0974.JPG 错误域= NSCocoaErrorDomain代码= 257“文件”IMG_0974.JPG“ 因为你没有权限查看它而无法打开。“ 的UserInfo = {NSURL =文件:///var/mobile/Media/DCIM/100APPLE/IMG_0974.JPG, NSFilePath =在/ var /移动/媒体/ DCIM / 100APPLE / IMG_0974.JPG, NSUnderlyingError = 0x14805d680 {错误域= NSPOSIXErrorDomain代码= 1 “不允许操作”}}
答案 0 :(得分:7)
我现在遇到同样的问题。上传在模拟器中工作正常,但在设备上它给我权限错误。我的解决方法是将图像上传为数据而不是文件的URL:
let imageData = UIImageJPEGRepresentation(image, 1.0)
let uploadTask = storageReference.child(remoteFilePath).putData(imageData, metadata: metadata, completion: { (metadata, error) in
// Your code here
}
答案 1 :(得分:1)
上传在模拟器中工作正常,但在设备上它给了我权限错误。 将图像上传为数据而不是文件的URL:
首先声明
fileprivate lazy var storageRef: StorageReference = Storage.storage().reference(forURL: "gs://yourAPP.appspot.com/")
然后
let path : String = "\(String(describing: Auth.auth().currentUser?.uid))/\(Int(Date.timeIntervalSinceReferenceDate * 1000))/\(photoReference)"
// 6
let imageData = UIImageJPEGRepresentation(photoReference, 0.1)
let uploadTask = self.storageRef.child(path).putData(imageData!, metadata: nil, completion: { (metadata, error) in
// Your code here
if let error = error {
print("Error uploading photo: \(error.localizedDescription)")
return
}
// 7
self.setImageURL(self.storageRef.child((metadata?.path)!).description, forPhotoMessageWithKey: key)
})
答案 2 :(得分:0)
谢谢你,还有亚历克斯和所有回答这个问题的人...... 我试过这个...下面这段代码在didFinishPickingMediaWithInfo中...... 我评论了当用户从photoLibrary中选择图像时处理的代码,并且再次感谢你,所以我的代码是(下面)
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
picker.dismissViewControllerAnimated(true,completion:nil)
let image = info[UIImagePickerControllerOriginalImage] as! UIImage//<--
let imageData = UIImageJPEGRepresentation(image, 1.0)
let imagePath = "photos/xxxyyyzzz.jpg"//whatever you want
let metadata = FIRStorageMetadata()
metadata.contentType = "image/jpeg"
storageRef.child(imagePath).putData(imageData!,metadata:metadata){
(metadata, error) in
if let error = error{
print("Error uploading photo: \(error)")
}
else{
//if there is no error the compiler will come here
print("no error")
//and maybe you want to use the full url or download url after success
//for example you wanna have tha downloadURL...
let downloadURL = metadata!.downloadURL()
print("\(downloadURL)")
//do whatever you want...
}
}
}
答案 3 :(得分:0)
另一种选择是将文件复制到“documents”目录(可写)。它使用了比UIImageJPEGRepresentation
方法更多的代码,因此您可能仍然更喜欢使用该技术。如果您真的不想改变原始图像,也许使用“文档目录”方法(但是,理论上,原始图像与使用asset.requestContentEditingInput(with: options, completionHandler: {(contentEditingInput: PHContentEditingInput?, info: [AnyHashable : Any]) -> Void in
let originalFileUrl = contentEditingInput!.fullSizeImageURL!
/* Copy file from photos app to documents directory */
let fileManager = FileManager.default
let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let destinationPath = documentsDirectory.appendingPathComponent(originalFileUrl.lastPathComponent)
let destinationUrl = URL(fileURLWithPath: destinationPath)
do {
try fileManager.copyItem(at: originalFileUrl, to: destinationUrl)
}
catch {
print("Unable to copy file from photos to documents directory (run out of space on device?)")
return
}
/* Create metadata etc. for Firebase... */
/* Upload file to Firebase */
fileRef.putFile(from: destinationUrl, metadata: metadata, completion: { (metadata, error) in
// Remove the file from the documents directory
do {
try fileManager.removeItem(at: destinationUrl)
}
catch {
print("Unable to remove file from documents directory")
}
})
})
创建的图像之间的差异应该是不可察觉的)。
{{1}}
答案 4 :(得分:-1)
查看Firebase存储规则。 和临时允许所有路径没有auth。