[英]Cannot retrieve file, error: The file couldn’t be opened because you don’t have permission to view it
The app that I am working has been working perfectly fine. 我工作的应用程序一直很好。 I am able to choose a picture from the library and upload to Firebase.
我可以从库中选择一张图片并上传到Firebase。 However, it suddenly does not allow me to upload the file from photo library.
但是,它突然不允许我从照片库上传文件。 From the simulator, there is no problem.
从模拟器,没有问题。 Only on my phone, I would receive that error.
只有在我的手机上,我才会收到这个错误。
the error that I received is 我收到的错误是
Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_8982.JPG” couldn’t be opened because you don’t have permission to view it."
UserInfo={NSURL=file:///var/mobile/Media/DCIM/138APPLE/IMG_8982.JPG,
NSFilePath=/var/mobile/Media/DCIM/138APPLE/IMG_8982.JPG,
NSUnderlyingError=0x12ed877f0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
This is weird as I have been doing it all the while but it suddenly stopped working today. 这很奇怪,因为我一直这样做,但它今天突然停止工作。
This is how my code looks like. 这就是我的代码的样子。
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// Clear data if picture is repicked
imageLocalURL = nil
imageData = nil
imageSelected = info[UIImagePickerControllerOriginalImage] as? UIImage
if info[UIImagePickerControllerReferenceURL] != nil {
// Image is selected from Photo Library
referenceURL = info[UIImagePickerControllerReferenceURL] as? NSURL
} else {
// Image is taken from camera
imageData = UIImageJPEGRepresentation(imageSelected!, 1.0)
}
transportImage.image = imageSelected
dismissViewControllerAnimated(true, completion: nil)
}
func saveDetails() {
if imageSelected != nil {
if referenceURL != nil {
// Image is from photo library
let assets = PHAsset.fetchAssetsWithALAssetURLs([referenceURL! as NSURL], options: nil)
let asset = assets.firstObject
let checkValidation = NSFileManager.defaultManager()
asset?.requestContentEditingInputWithOptions(nil, completionHandler: { (contentEditingInput, info) in
self.imageLocalURL = contentEditingInput?.fullSizeImageURL
// I did this to check if there is a local URL and if the file exist, but it returns false
print(self.imageLocalURL)
print(checkValidation.fileExistsAtPath("\(self.imageLocalURL)"))
self.uploadTask = storageRef.child(filePath).putFile(self.imageLocalURL!, metadata: nil) { (metadata, error) in
if let error = error {
print("Error uploading: \(error.description)")
return
}
}
} else if imageData != nil {
let imageLocalData = UIImageJPEGRepresentation(imageSelected!, 0.8)
let metadata = FIRStorageMetadata()
metadata.contentType = "image/jpeg"
uploadTask = storageRef.child(filePath).putData(imageLocalData!, metadata: metadata) { (metadata, error) in
if let error = error {
print("Error uploading: \(error)")
return
}
}
UIImageWriteToSavedPhotosAlbum(imageSelected!, nil, nil, nil)
}
If the image is taken from the camera, there is no problem. 如果从相机拍摄图像,则没有问题。 If the image is taken from photo library, I receive that error.
如果图像是从照片库中拍摄的,我会收到该错误。
I searched around and I have tried Option+Clean
the build. 我四处搜索,我尝试了
Option+Clean
构建。 I went to Projects->derived data-> delete
I checked the compiler is default. 我去了
Projects->derived data-> delete
我检查编译器是默认的。
Could anyone assist me with this issue? 任何人都可以帮我解决这个问题吗?
I'm guessing you're getting denied access once you're outside the scope of didFinishPickingMediaWithInfo. 一旦你超出了didFinishPickingMediaWithInfo的范围,我猜你会被拒绝访问。
Maybe you can pull the image from it, instead of keeping the URL? 也许你可以从中提取图像,而不是保留URL?
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
Edit: 编辑:
Sorry, didn't notice you were in Swift, and that you already pull the image. 对不起,没有注意到你在Swift,你已经拉了图像。 Still, my suggestion stands.
不过,我的建议仍然存在。 Keep the image around instead of the URL, and you should be fine.
保持图像而不是URL,你应该没问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.