Ios 选择后图像将不会显示在图像视图中

Ios 选择后图像将不会显示在图像视图中,ios,sqlite,core-data,uiimageview,picker,Ios,Sqlite,Core Data,Uiimageview,Picker,我试图在core data中保存一个图像,但在模拟器中选择它后,它不会显示在图像视图中?以下是代码示例: - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (fugitive_.image != nil) { self.fugitiveImage.image = [UIImage imageWithData:fugitive_.image]; } }

我试图在core data中保存一个图像,但在模拟器中选择它后,它不会显示在图像视图中?以下是代码示例:

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    if (fugitive_.image != nil) {
        self.fugitiveImage.image = [UIImage imageWithData:fugitive_.image];
    }
}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
   fugitive.image = UIImagePNGRepresentation([info objectForKey:UIImagePickerControllerEditedImage]);
    [self dismissModalViewControllerAnimated:YES];
    [picker release];
}

首先,不要在核心数据中存储图像或任何二进制数据;特别是在iOS上。将其存储在磁盘上,然后在核心数据中存储对文件位置的引用,将获得更好的性能

其次,示例代码没有显示如何将数据放入核心数据。因此,很难提出解决方案

使现代化 我没有找到一个简单的参考方法,因此这里有一个:

iOS 5.0之前的图像缓存 要在iOS 5.0之前的环境中在磁盘上设置映像缓存,首先要在实体上创建一个NSString属性。在本例中,我们将该属性命名为imageFilename。完成后,我们希望为实体创建NSManagedObject的子类,以便实现帮助器方法:

@interface MyEntity : NSManagedObject

@property (nonatomic, retain) NSString *imageFilename;
@property (nonatomic, retain) NSImage *image;

@end
我们将让核心数据管理imageFilename,因为它是在模型中定义的。然而,我们将实现图像的访问器

-setImage:将把映像保存到磁盘的缓存目录中。请注意,缓存目录没有备份,在空间不足的情况下,系统可以删除该目录。如果尚未创建文件名,则会选择一个随机文件名

故意不存储路径,因为应用程序沙盒的目录可能会更改。因此,我们只希望在核心数据中存储文件名并解析路径

我们还保留对映像的内存引用,以便在该实体的生命周期中,如果再次请求映像,我们不会访问磁盘。这就是@synthesis的原因,尽管我们正在实现访问器

请注意,我们将图像以PNG格式存储在磁盘上。这可能是昂贵的压缩程序相对缓慢,但它保持在一个通用格式的图像,这可能是有用的

- (UIImage*)image
{
    if (_image) return _image;
    NSString *filename = [self imageFilename];
    if (!filename) return nil;
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [cachePath stringByAppendingPathComponent:filename];

    if (![[NSFileManager defaultFileManager] fileExistsAtPath:filePath]) return nil;

    _image = [[UIImage alloc] initWithContentsOfFile:filePath];
    return _image;
}
映像的实现几乎是相反的。我们检查是否有文件名;解析完整路径并将图像加载到内存中,然后将其返回给调用者

- (void)prepareForDeletion
{
    [super prepareForDeletion];

    NSString *filename = [self imageFilename];
    if (!filename) return nil;
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [cachePath stringByAppendingPathComponent:filename];

    NSError *error = nil;
    if (![[NSFileManager defaultFileManager] removeItemAtPath:filePath error:&error]) {
        NSLog(@"Potential error removing on disk image: %@\n%@", [error localizedDescription], [error userInfo]);
    }
}
我们希望保持缓存目录尽可能干净,这样就不会造成空间不足的情况。因此,当要从核心数据中删除实体时,我们希望从磁盘中删除该文件。在删除过程中发生的实际错误对我们来说不是致命的问题。这可能是一个错误,因为该文件已被删除或其他内容。没有理由因为这个错误而完全失败,但是记录它是很重要的

- (void)willTurnIntoFault
{
    [super willTurnIntoFault];
    [_image release], _image = nil;
}

@end
最后,我们实现了-willTurnIntoFault方法,以便在该实体的生命周期结束时释放对该映像的内存引用

图像缓存iOS 5.0+ 在实体上创建二进制属性。 打开存储在外部记录文件位。 没有第三步
那你能给我指一本关于如何保存磁盘的教程吗?我有一个tableview应用程序,对于tableview中的每个对象,我都想保存一些图片。@GeorgeCc用一个例子更新了答案。自始至终都是一个很好的例子。我喜欢你扩展NSManagedObject并清理一切的方式。
- (void)willTurnIntoFault
{
    [super willTurnIntoFault];
    [_image release], _image = nil;
}

@end