【发布时间】:2017-02-06 04:43:37
【问题描述】:
我正在尝试将图像保存到 Swift 3 中的照片库(我正在使用 Xcode 8)。
ViewController 代码:
func shareImage(image: UIImage) {
let items = [image]
var activityVC: UIActivityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)
let excludeActivities: [UIActivityType] = [UIActivityType.airDrop,
UIActivityType.assignToContact,
UIActivityType.addToReadingList,
UIActivityType.copyToPasteboard]
activityVC.excludedActivityTypes = excludeActivities
self.present(activityVC, animated: true, completion: nil)
}
当我运行应用程序并单击按钮截取屏幕截图(将其转换为图像,...,一切正常),应用程序请求访问照片库的权限,我点击“确定"按钮,然后应用程序崩溃。图片未保存在照片库中。
我从 Xcode 得到的唯一线索如下:
2016-09-28 11:24:27.216043 Ajax Kids[4143:1545362] [error] error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///var/mobile/Media/PhotoData/Photos.sqlite?readonly_shm=1 options:{
NSPersistentStoreFileProtectionKey = NSFileProtectionCompleteUntilFirstUserAuthentication;
NSReadOnlyPersistentStoreOption = 1;
NSSQLitePersistWALOption = 1;
NSSQLitePragmasOption = {
"journal_mode" = WAL;
};
} ... returned error Error Domain=NSCocoaErrorDomain Code=256 "The file couldn’t be opened." UserInfo={reason=Failed to access file: 1} with userInfo dictionary {
reason = "Failed to access file: 1";
}
2016-09-28 11:24:27.216433 Ajax Kids[4143:1545362] [Migration] Unexpected error opening persistent store <private>, cannot attempt migration <private>)
2016-09-28 11:24:27.216568 Ajax Kids[4143:1545362] [Migration] Failed to open store <private>. Requires update via assetsd (256: <private>)
有人知道如何解决这个问题吗?
提前致谢!
更新
在社交媒体上分享图片效果很好,因此问题被指定为将图片保存在照片库中。
【问题讨论】:
-
尝试保存图片时分享代码
-
@OlegGordiichuk 这就是我为共享/保存图像而编写的所有代码。例如,操作系统负责保存代码或在 Facebook 上发布图像。这是您不需要使用 UIAcrtivityViewController 编写的代码。
-
您应该在 plait 文件中添加权限
-
@OlegGordiichuk 去过那里,做到了:隐私 - 照片库使用说明已添加并填写在 info.plist 中
-
@OlegGordiichuk 是的,(顺便说一句:隐私 - 照片库使用说明(在 info.plist 的属性列表中)与 NSPhotoLibraryUsageDescription 完全相同(在 info.plist 的源代码中))
标签: swift ios10 xcode8 uiactivityviewcontroller save-image