[英]UIImagePickerController - how to save image to core data? (Swift)
我想保存從ImagePicker中拾取的圖像,並將其保存在CoreData中。 我在具有Binary Data類型的實體中創建Attribute。
首先,您需要將UIImage
轉換為Data
:
let data = UIImageJPEGRepresentation(image, 1.0)
之后,在NSManagedObjectContext
的perform
方法中將此數據設置為Entity
中的屬性:
context.perform {
entity.image = data
// Save context
}
我不建議您將UIImage保存到CoreData,這是不好的方法。 只需使用https://github.com/Alamofire/AlamofireImage即可正確緩存UIImage。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.