![](/img/trans.png)
[英]The file “MyApp” couldn’t be opened because you don’t have permission to view it error (Xcode 11)
[英]Cannot retrieve file, error: The file couldn’t be opened because you don’t have permission to view it
我工作的应用程序一直很好。 我可以从库中选择一张图片并上传到Firebase。 但是,它突然不允许我从照片库上传文件。 从模拟器,没有问题。 只有在我的手机上,我才会收到这个错误。
我收到的错误是
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"}}
这很奇怪,因为我一直这样做,但它今天突然停止工作。
这就是我的代码的样子。
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)
}
如果从相机拍摄图像,则没有问题。 如果图像是从照片库中拍摄的,我会收到该错误。
我四处搜索,我尝试了Option+Clean
构建。 我去了Projects->derived data-> delete
我检查编译器是默认的。
任何人都可以帮我解决这个问题吗?
一旦你超出了didFinishPickingMediaWithInfo的范围,我猜你会被拒绝访问。
也许你可以从中提取图像,而不是保留URL?
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
编辑:
对不起,没有注意到你在Swift,你已经拉了图像。 不过,我的建议仍然存在。 保持图像而不是URL,你应该没问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.