Swift Core Data 图片存储与读取

 

1.首先推出选择拍照还是相册的alert,代码如下:


UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle: UIAlertControllerStyleActionSheet];

UIAlertAction *photo = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

[self readImageFromCamera];

}];

UIAlertAction *albulm = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

[self readImageFromAlbum];

}];

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

[self dismissViewControllerAnimated:YES completion:nil];

}];

[alert addAction:photo];

[alert addAction:albulm];

[alert addAction:cancel];

[self presentViewController:alert animated:YES completion:nil];


2.创建UIImagePickerController,设置imagepicker的sourcetype为camera还是library,设置代理,设置imagepicker的alllowsediting为yes;

3.在imagepicker的代理方法(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediawithInfo:(NSDictionary*)info中,取出想要的照片,为info[UIImagePickerControllerEditedImage];info的key海域IImagePickerControllerOriginalImage;

4.对图片进行相应size的压缩


image = [self scaleImage:image toSize:CGSizeMake(750, 750)];

- (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)size {

if (image.size.width > size.width) {

UIGraphicsBeginImageContext(size);

[image drawInRect:CGRectMake(0, 0, size.width, size.height)];

UIImage *newImage = UIGraphicsGetimageFromCurrentimageContext();

UIGraphicsEndImageContext();

return newImage;

} else {

return image;

}

}

实体的模型定义:

选择类型 


实体的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)
}
原文链接:https://blog.csdn.net/zhangao0086/article/details/44499405

 

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...