我想允许用户拍照并将这张照片附加到将存储在 CoreData 中的“人”的个人资料中。
存储此 UIImage 的典型方式是什么?
已编辑
总的来说,每个表(“Person”)将包含一些字符串和这个 UIImage。
我不希望 UIImages 占用太多空间,所以当用户拍照时,我会将其裁剪并尽可能小地保存,而不会失去视觉完整性。但这是另一种方法,以后再弄清楚。
知道这些 UIImage 的大小范围是多少吗?我只需要每个个人资料的头像照片。就像在 iPhone 上的“联系人”应用程序中一样,我也希望能够通过 iMessage 或电子邮件与其他人分享这个“人”。
那么关于是否需要将整个“人”文件保存下来的任何建议
100kb store in the same table as the relevant data
1mb store in a separate table attached via a relationship to avoid loading unnecessarily
1mb store on disk and reference it inside of Core Data
最佳答案
这里的代码将帮助您将 UIImage
存储在文档目录
中,然后将文档路径
存储到您的数据库中。
let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask
if let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) {
if paths.count > 0 {
if let dirPath = paths[0] as? String {
let writePath = dirPath.stringByAppendingPathComponent("Image2.png")
UIImagePNGRepresentation(image).writeToFile(writePath, atomically: true)
}
}
}
希望对你有帮助。
关于ios - 使用 swift 存储在应用程序中拍摄的 UIImages 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29788013/