将图像数据保存到核心数据但再次启动模拟器,它已经消失了

时间:2013-12-29 07:38:56

标签: ios objective-c core-data uiimage

我已将图像数据保存到核心数据,关键是二进制数据格式。保存到核心数据后,我使用valueForKey:在UIImageView中显示图像。但是在我再次启动模拟器之后,一切都仍然保存,但是图像数据消失了,当我对NS的数据描述进行NSLog时,它是空的。

NSDictionary *show = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
Summary *summary = [NSEntityDescription insertNewObjectForEntityForName:@"Summary" inManagedObjectContext:weakSelf.managedObjectContext];
summary.title = show[@"title"];
summary.poster = show[@"images"][@"poster"];

SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:[NSURL URLWithString:summary.poster] options:SDWebImageProgressiveDownload progress:^(NSUInteger receivedSize, long long expectedSize) {

} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
    summary.posterImageData = data;
}];

[weakSelf.managedObjectContext dct_saveWithCompletionHandler:^(BOOL success, NSError *error) {
    if (success) {
    } else {
        NSLog(@"%@", error);
    }
}];

2 个答案:

答案 0 :(得分:0)

我认为你当前的代码,在dct_save WithCompletionHandler之后调用了downloadWithURL的完整块,试试:

SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:[NSURL URLWithString:summary.poster] options:SDWebImageProgressiveDownload progress:^(NSUInteger receivedSize, long long expectedSize) {

} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
    summary.posterImageData = data;
    [weakSelf.managedObjectContext dct_saveWithCompletionHandler:^(BOOL success, NSError *error) {
        if (success) {
        } else {
            NSLog(@"%@", error);
        }
    }];    
}];

答案 1 :(得分:0)

确保您实际保存了托管对象上下文。请仔细检查您是否也正确设置了MOC的并发类型。