ios - 跨多个 iOS 设备访问图像
问题描述
我让用户为应用选择自己的背景图片。此图像假设使用 iCloud 跨设备同步,但我无法让它工作。图像似乎已保存,保存它的设备可以访问,但其他设备无法访问该图像。这是我进行所有设置的方式:
static func setBackgroundImage(image: UIImage?) {
if let image = image {
let notification = Notification(name: updatedBackgroundImageNotificationName, object: nil, userInfo: ["image": image])
NotificationCenter.default.post(notification)
} else {
NotificationCenter.default.post(name: updatedBackgroundImageNotificationName, object: nil)
}
guard let directoryUrl = CloudSettings.resourcesDirectoryUrl,
let fileUrl = CloudSettings.backgroundImageUrl else {
print("Unable to save background")
return
}
var isDirectory = ObjCBool(true)
if !FileManager.default.fileExists(atPath: directoryUrl.path, isDirectory: &isDirectory) {
do {
try FileManager.default.createDirectory(at: directoryUrl, withIntermediateDirectories: true, attributes: nil)
} catch let error {
print(error)
return
}
}
let stop = fileUrl.startAccessingSecurityScopedResource()
defer {
if stop {
fileUrl.stopAccessingSecurityScopedResource()
}
}
isDirectory = ObjCBool(false)
if FileManager.default.fileExists(atPath: fileUrl.path, isDirectory: &isDirectory) {
do {
try FileManager.default.removeItem(at: fileUrl)
} catch let error {
print(error)
return
}
}
if let imageData = image?.pngData() {
do {
try imageData.write(to: fileUrl)
} catch let error {
print(error)
return
}
}
}
static func getBackgroundImage() -> UIImage? {
guard let url = CloudSettings.backgroundImageUrl else {
print("Unable to get background image")
return nil
}
let stop = url.startAccessingSecurityScopedResource()
defer {
if stop {
url.stopAccessingSecurityScopedResource()
}
}
var isDirectory = ObjCBool(false)
if FileManager.default.fileExists(atPath: url.path, isDirectory: &isDirectory) {
return UIImage(contentsOfFile: url.path)
}
return nil
}
是我遗漏了什么还是我的代码有问题?
解决方案
推荐阅读
- postgresql - 连接到 sftp 文件夹,根据上次修改对文件进行排序,并将最新文件的内容转储到 RDS
- r - 使用 Cairo 导出为 SVG 时面板边框宽度不一致
- python - 如何将复数矩阵的索引相乘以获得新的复数矩阵
- bash - 使用制表符分隔文件中字段的空格分隔参数
- swift - 第三次返回视图时 ARView 崩溃
- python - 如何使用 python 和 scapy sniff() 和 send() 模拟以太网交换机
- c++ - 类似于 mp-units“向下转换工具”的类型的强别名,仅使用 C++14
- html - Bootstrap 5 列堆叠取决于宽度
- c# - 我如何才能在 C# 中只获取异常堆栈跟踪消息的第一行?
- javascript - ReactJS:超出最大部门/私人路线