错误域 = NSCocoaErrorDomain 代码 = 257 文件“”无法打开 - Firebase
Posted
技术标签:
【中文标题】错误域 = NSCocoaErrorDomain 代码 = 257 文件“”无法打开 - Firebase【英文标题】:Error Domain=NSCocoaErrorDomain Code=257 The fil "" could not be opened - Firebase 【发布时间】:2017-09-03 17:17:53 【问题描述】:所以我通过自定义 UICollectionView 访问用户的视频和照片,我的问题是当我尝试通过手机将视频上传到 firebase 时,我收到此错误:
2017-09-03 13:09:20.884509-0400 Project[5797:2021536] Cannot get file size: Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_3476.MP4” couldn’t be opened because you don’t have permission to view it." UserInfo=NSURL=file:///var/mobile/Media/DCIM/103APPLE/IMG_3476.MP4, NSFilePath=/var/mobile/Media/DCIM/103APPLE/IMG_3476.MP4, NSUnderlyingError=0x17064f450 Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"
/var/mobile/Media/DCIM/103APPLE/IMG_3476.MP4
2017-09-03 13:09:21.261767-0400 Project[5797:2021536] Body file is unreachable: /var/mobile/Media/DCIM/103APPLE/IMG_3476.MP4
Error Domain=NSCocoaErrorDomain Code=257 "The file “IMG_3476.MP4” couldn’t be opened because you don’t have permission to view it." UserInfo=NSURL=file:///var/mobile/Media/DCIM/103APPLE/IMG_3476.MP4, NSFilePath=/var/mobile/Media/DCIM/103APPLE/IMG_3476.MP4, NSUnderlyingError=0x170651b20 Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"
这里是我加载文件的地方
struct Media
var image:UIImage?
var videoURL:NSURL?
var mediaArray = [Media]()
func grabPhotos()
let imgManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
requestOptions.deliveryMode = .highQualityFormat
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions)
if fetchResult.count > 0
for i in 0..<fetchResult.count
var mediaItem = Media()
//Used for fetch Image//
imgManager.requestImage(for: fetchResult.object(at: i) as PHAsset , targetSize: CGSize(width: 400, height: 400), contentMode: .aspectFit, options: requestOptions, resultHandler:
image, error in
let imageOfVideo = image! as UIImage
mediaItem.image = imageOfVideo;
//Used for fetch Video//
imgManager.requestAVAsset(forVideo: fetchResult.object(at: i) as PHAsset, options: PHVideoRequestOptions(), resultHandler: (avAsset, audioMix, info) -> Void in
if let asset = avAsset as? AVURLAsset
let videoData = NSURL(string: "\(asset.url)")
let duration : CMTime = asset.duration
let durationInSecond = CMTimeGetSeconds(duration)
print(durationInSecond)
mediaItem.videoURL = videoData!
self.mediaArray.append(mediaItem)
print(self.mediaArray.count)
)
)
else
//showAllertToImportImage()//A function to show alert
我认为这与我上传视频的时间无关,因为我可以录制视频并从手机上传,而不是从手机本身上传;我只是将 url 转移到 nextVC,如下所示:
let videoVC = PrepareVideoVC()
videoVC.url = outputFileURL as URL!
self.navigationController?.pushViewController(videoVC, animated: false)
我研究了这个The file “ ” couldn’t be opened because you don’t have permission to view it,但无法真正弄清楚如何实现它
我也遇到了https://***.com/a/41517165/7823620,但我再次尝试实现它,但无法让它不给我错误
我也可以从模拟器上传,但不能从手机上传
它正在尝试从此目录获取文件:
NSFilePath=/var/mobile/Media/DCIM/
【问题讨论】:
你找到解决办法了吗?我面临同样的问题 不幸的是我没有,我目前只是使用原生 UIImagePicker 【参考方案1】:你可以试试
url.startAccessingSecurityScopedResource()
<...>
url.stopAccessingSecurityScopedResource()
【讨论】:
非常感谢您!一直很想弄清楚这个!奇迹般有效! :) 只是在上面添加了 Ivan 的评论,因为它为我解决了一个问题。见下文。【参考方案2】:使用此选项:
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
options.version = PHVideoRequestOptionsVersionCurrent;
options.deliveryMode = PHVideoRequestOptionsDeliveryModeHighQualityFormat;
options.networkAccessAllowed = true;
如果你请求中等质量的视频,那么你会得到一个错误“Error Domain=NSCocoaErrorDomain Code=257, no permission...”
【讨论】:
【参考方案3】:因此,如果 AppDelegate 在启动时通过此方法传入文档:
func application(_: UIApplication, open url: URL, options _: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool
我发现我必须这样做才能修复它,以便使用上述 Ivan 的解决方案成功加载 FileManager 文档,但如果说文档是电子邮件附件,则无法进行该调用。
这为我解决了这个问题,再次感谢 Ivan Vavilov 的帖子。
func application(_: UIApplication, open url: URL, options _: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool
print("Doc attachment passed")
let docAttachment = url
// files written to 'tmp' are NOT backed up to iCloud or iTunes
let tempDir = getTempDirectory()
// add file name to tmp path
tempFileToLoad = tempDir.appendingPathComponent(docAttachment.lastPathComponent)
let fm = FileManager()
// passed a file from the FileManger app?
let isAttachmentScopedResource = docAttachment.startAccessingSecurityScopedResource()
do
// put in tmp so we can read/write
try fm.copyItem(at: docAttachment, to: tempFileToLoad!)
if(isAttachmentScopedResource)
// have MUST release the security access after calling it
docAttachment.stopAccessingSecurityScopedResource()
catch
print("Error getting file. \(error)")
return false
【讨论】:
以上是关于错误域 = NSCocoaErrorDomain 代码 = 257 文件“”无法打开 - Firebase的主要内容,如果未能解决你的问题,请参考以下文章
Json 错误:错误域 = NSCocoaErrorDomain 代码 = 3840“无值”。 UserInfo=NSDebugDescription=无值。
错误域 = NSCocoaErrorDomain 代码 = 3840 “字符周围的值无效
iOS swift 中的错误域 = NSCocoaErrorDomain 代码 = 3840?
iOS:- 错误:无法创建存储目录。错误域 = NSCocoaErrorDomain 代码 = 512
如何修复“错误域 = NSCocoaErrorDomain 代码 = 3840”无价值。“UserInfo = NSDebugDescription = 无价值。”