实体的模型定义:
实体的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所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)