【问题标题】:how to store image to sqlite using core data in iphone? [duplicate]如何使用 iphone 中的核心数据将图像存储到 sqlite? [复制]
【发布时间】:2011-06-30 08:29:14
【问题描述】:

可能重复:
how to store thumbnail image to sqlite table using core data in iphone?

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    // Check if it's a photo or video and create MediaItem accordingly
    if([[info valueForKey:@"UIImagePickerControllerMediaType"] isEqualToString:(NSString *)kUTTypeImage]){
        NSLog(@"Image");
        PhotoItem *photo = [NSEntityDescription insertNewObjectForEntityForName:@"PhotoItem" inManagedObjectContext:context];
        [photo setImage:[info valueForKey:@"UIImagePickerControllerOriginalImage"]];
        currentItem = [photo retain];
//        listcell=[NSEntityDescription insertNewObjectForEntityForName:@"BucketListItem" inManagedObjectContext:context];
//        [listcell setItemText:@"Photo"];


        nameAlert = [[UIAlertView alloc] initWithTitle:@"Enter Photo Name" message:@" " delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];
        /*CGAffineTransform transform = CGAffineTransformMakeTranslation(0, 150);
        [nameAlert setTransform:transform];*/
        nameField = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, 245, 20)];
        nameField.backgroundColor = [UIColor whiteColor];
        nameField.autocapitalizationType = UITextAutocapitalizationTypeWords;
        nameField.delegate = self;
        [nameAlert addSubview:nameField];
        [nameAlert show];
        [nameAlert release];
    }

我正在使用上面的代码来存储照片库中的图像。我需要将缩略图存储到数据库中。如何做到这一点?

【问题讨论】:

  • 我这里没有错误。我不知道如何存储图像,所以我用代码询问。
  • 如果你想使用Core Data,首先你必须学习Core Data。然后编写一个模型,用一百行样板代码初始化 Core Data,编写一个缩略图类,将图像以二进制形式保存到其中并保存更改。说明太长,无法在此处发布。另一种方法是将文件写入磁盘并将路径保存到 plist。我会使用文件的 md5 作为文件名。
  • 我正在根据需要逐步学习核心数据。我将尝试将文件写入磁盘并将该路径保存到 plist 的替代方法。
  • 这是你第三次问这个问题:12Please don't do that.

标签: objective-c cocoa-touch ios4 core-data


【解决方案1】:

Core Data 存储图片有两种方法:

(1) 将 Core Data 实体属性设置为“可转换”类型。可转换属性接受任何可以将其转换为保存到持久存储的原始数据的对象。当您读取属性时,它会反转该过程。由于 UIImage 没有实现 NSCoding 协议,因此您必须提供自定义值转换器。有关介绍,请参阅核心数据编程指南:Non-Standard Persistent Attributes

(2) 但是,(1) 的系统开销相当高,因此您通常不会将图像直接存储在持久存储中,而是将它们存储为外部图像文件,然后仅将文件路径存储在 Core Data 中。这更快,内存占用更少。

大多数人使用选项 (2)。 Core Data 新手也更容易实现。

【讨论】:

    猜你喜欢
    • 2011-01-06
    • 2013-05-17
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    相关资源
    最近更新 更多