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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】