Mur*_*eek 1 core-data objective-c ios5.1
我从照片库中获取图像并将其保存到核心数据,然后将其显示到图像视图.这是正确的工作.但是当我再次运行项目时,图像没有显示在我的图像视图上.
这是从照片库中获取图像的代码
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
UIImage *img= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSManagedObjectContext *context=[self managedObjectContext];
NSManagedObject *newContact=[[NSManagedObject alloc] initWithEntity:[NSEntityDescription entityForName:@"Contacts" inManagedObjectContext:context] insertIntoManagedObjectContext:context];
//newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Images" inManagedObjectContext:context];
NSData *data=UIImageJPEGRepresentation(img,0.0);
[newContact setValue:data forKey:@"image"];
[self fetchData];
Run Code Online (Sandbox Code Playgroud)
}
这是fetchData()方法的代码.
-(void)fetchData
{
NSManagedObjectContext *context=[self managedObjectContext];
NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Contacts"
inManagedObjectContext:context]];
NSError * error = nil;
NSArray * objects = [context executeFetchRequest:request error:&error];
if ([objects count] > 0) {
NSManagedObject *managedObject = objects[0];
data1 = [managedObject valueForKey:@"image"];
image2 = [[UIImage alloc]initWithData:data1];
_imageView.image=image2;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的viewDidLoad()
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchData];
// Do any additional setup after loading the view.
}
Run Code Online (Sandbox Code Playgroud)
首先,您应该将图像路径(不是uiimage)保存到Core Data,然后从Core Data获取路径并通过此路径将图像加载到imageView.
并使用Magical Record(https://github.com/magicalpanda/MagicalRecord)来管理核心数据,您将完全没有问题.
归档时间: |
|
查看次数: |
84 次 |
最近记录: |