Swift Core Data 图片存储与读取Demo

Swift Core Data 图片存储与读取Demo,第1张

概述实体的模型定义: 实体的class定义: @objc(ImageEntity)class ImageEntity: NSManagedObject { @NSManaged var imageData: NSData} 存储: @IBAction func saveImageToCoreData() { let delegate = UIApplication.sharedAp

实体的模型定义:

实体的class定义:

@objc(ImageEntity)class ImageEntity: NSManagedobject {    @NSManaged var imageData: NSData}

存储:

@IBAction func saveImagetoCoreData() {    let delegate = UIApplication.sharedApplication().delegate as AppDelegate    let context = delegate.managedobjectContext    let imageData = UIImagePNGRepresentation(UIImage(named: "image"))    let imageEntity = NSEntityDescription.entityForname("ImageEntity",inManagedobjectContext: context!)    let image = ImageEntity(entity: imageEntity!,insertIntoManagedobjectContext: context!)    image.imageData = imageData    var error: NSError?    if context!.save(&error) == false {        println("Failed: \(error!.localizedDescription)")    }}

读取

@IBAction func loadImageFromCoreData() {    let delegate = UIApplication.sharedApplication().delegate as AppDelegate    let context = delegate.managedobjectContext    let request = NSFetchRequest(entityname: "ImageEntity")    var error: NSError?    let imageEntitIEs = context?.executeFetchRequest(request,error: &error)    let imageEntity = imageEntitIEs?.first! as ImageEntity    self.imageVIEw.image = UIImage(data: imageEntity.imageData)}
Demo地址 总结

以上是内存溢出为你收集整理的Swift Core Data 图片存储读取Demo全部内容,希望文章能够帮你解决Swift Core Data 图片存储与读取Demo所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1088523.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存