[英]iOS 9 read file permission
在iOS 9+中,任何从文件读取的尝试均无效。 在这种情况下,该文件是图像文件路径。
使用
NSData(contentsOfFile: stringpath, options: NSDataReadingOptions.DataReadingUncached)
要么
NSData(contentsOfFile: stringpath)
动作:
我已经从路径中删除了“ file://”,并且现在存在权限问题。
错误域= NSCocoaErrorDomain代码= 257“无法打开文件“ IMG_0048.JPG”,因为您没有查看权限。” UserInfo = {NSFilePath = / var / mobile / Media / DCIM / 100APPLE / IMG_0048.JPG,NSUnderlyingError = 0x13f978f50 {Error Domain = NSPOSIXErrorDomain Code = 1“不允许操作”}}
我添加了NSAllowArbitraryLoads
并将其设置为true。
我试图使用“ NSSearchPathDirectory ”自己查找文件,但是路径完全不匹配
我遇到此错误是因为我试图访问同一块中的多个文件。 对我有用的解决方案是更改代码结构,以便在尝试获取下一个文件url之前先获取每个文件的url,然后从中读取。
您很可能会收到此错误,因为iOS应用程序只能访问其沙箱中的文件。 有关详细信息,请参阅文件系统上的Apple 文档 。
在您的应用程序中,由于沙箱操作,您无权访问/var/mobile/Media/DCIM/100APPLE/IMG_0048.JPG
文件。
因此,无论您做什么,都无法使用文件路径初始化NSData
或UIImage
。 但是,你可以访问的文件/var/mobile/Media/DCIM/100APPLE/xxx.mov
与AVURLAsset
。 在我的应用程序中,我通过“照片”工具包从库中提取了数据而不是URL,并使用数据初始化了UIImage
。
PHImageManager.default().requestImageData(
for: assetObject!, options: options,
resultHandler: {
data, _, _, _ in
if data != nil {
self.assetUrl = movieMaker.createMovieFrom(imageData: data!, duration: Int(CXPreparetValue.imageDuration))
}
})
这个对我有用! 如果您有其他意见,请告诉我。
就我而言,文件权限限制太严格,因此我无法读取文件。
在访问文件之前向文件添加读写权限可以解决该问题。
do {
// Retrieve any existing attributes
var attrs = try FileManager.default.attributesOfItem(atPath: stringpath)
let existing = (attrs as NSDictionary).filePosixPermissions()
// Set the read+write value in the attributes dict
attrs[.posixPermissions] = existing | 0b110000000
// Update attributes
try FileManager.default.setAttributes(attrs, ofItemAtPath: stringpath)
// Read data from file
let data = try Data(contentsOf: URL(fileURLWithPath: stringpath, isDirectory: false), options: .uncached)
print("success: \(data.count)")
} catch {
print(error)
}
如果您位于具有足够权限的文件夹中,则可以使用该功能,因为即使您之前没有对该文件的读取权限,也可以更改文件权限。 此解决方案已在https://github.com/ZipArchive/ZipArchive/issues/293中应用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.