我在将图像复制到临时文件夹然后从中读出时遇到问题。我可以将图像复制到临时文件夹(不会抛出任何错误)。我试图读出文件夹中的项目列表。我收到的错误是
Error Domain=NSCocoaErrorDomain Code=256 "The file “21C6331F-609C-4719-ADCE-2B76B6C2EA3A” couldn’t be opened." UserInfo={NSUserStringVariant=( Folder ), NSFilePath=/private/var/mobile/Containers/Data/Application/*******/tmp/21C6331F-609C-4719-ADCE-2B76B6C2EA3A, NSUnderlyingError=0x282f0a070 {Error Domain=NSPOSIXErrorDomain Code=20 "Not a directory"}}
这是我的代码:
func getNotificationImageUrl() -> URL? {
guard let _documentPath = self.documentPath else{
return nil
}
do {
// Get the directory contents urls (including subfolders urls)
let directoryContents = try FileManager.default.contentsOfDirectory( at:_documentPath, includingPropertiesForKeys: nil, options: [])
// filter results get from folder
let imageurl = directoryContents.first(where: { $0.lastPathComponent.contains("banner")})
if let imageurl = imageurl {
print("[DEBUG] [\(#function)] [\(#line)] imageurl : \(imageurl)")
// create a temp directory
let tempDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString, isDirectory: true)
// copy item from directoryContents to temp folder
try FileManager.default.copyItem(at: imageurl, to: tempDirectoryURL)
// check whether copy item is successful
/************The throw here **************/
let items = try FileManager.default.contentsOfDirectory(atPath: tempDirectoryURL.path)
for item in items {
print("[DEBUG] [\(#function)] [\(#line)] Found \(item)")
}
return tempDirectoryURL
}
} catch let error as NSError {
print("[DEBUG] [\(#function)] [\(#line)] getNotificationImageUrl : \(error) ")
}
return nil
}
目录目录示例:
[ file:///private/var/mobile/Containers/Data/Application/*******/Library/Application%20Support/Advertisement/a68j6fc15abcdc1/banner.png, file:///private/var/mobile/Containers/Data/Application/*******/Library/Application%20Support/Advertisement/a68j6fc15abcdc1/a68j6fc15abcdc1.zip, file:///private/var/mobile/Containers/Data/Application/*******/Library/Application%20Support/Advertisement/a68j6fc15abcdc1/a68j6fc15abcdc1.json, file:///private/var/mobile/Containers/Data/Application/*******/Library/Application%20Support/Advertisement/a68j6fc15abcdc1/launch.jpg ]
谢谢。
希望这段代码和解释可以帮助您了解情况。 联合国通知附件 :
系统在显示相关通知之前验证附件。如果您将文件附加到损坏、无效或文件类型不受支持的本地通知请求,系统不会安排您的请求。对于远程通知,系统会在您的通知服务应用程序扩展完成后验证附件。一旦通过验证,系统会将附加文件移动到附件数据存储中,以便适当的进程可以访问这些文件。系统复制位于应用程序包内的附件。
添加附件后,它将从我现有的文件夹中删除。 之前尝试过从 UIImage 转换为 JPEGData 的方法,在后台模式下处理转换和将数据写入文件需要很长时间。