【问题标题】:Saving pictures taken in application and using core data保存在应用程序中拍摄的照片并使用核心数据
【发布时间】:2013-06-25 15:34:29
【问题描述】:

我正在使用核心数据来保存我的应用程序中包含的一组图像的文件名。

 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
{
    // app already launched
}
else
{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    // This is the first launch ever
        NSArray *mainDishImages = [NSArray arrayWithObjects:@"egg_benedict.jpg", @"full_breakfast.jpg", @"ham_and_cheese_panini.jpg", @"ham_and_egg_sandwich.jpg", @"hamburger.jpg", @"instant_noodle_with_egg.jpg", @"japanese_noodle_with_pork.jpg", @"mushroom_risotto.jpg", @"noodle_with_bbq_pork.jpg", @"thai_shrimp_cake.jpg", @"vegetable_curry.jpg", nil];
        NSArray *drinkDessertImages = [NSArray arrayWithObjects:@"angry_birds_cake.jpg", @"creme_brelee.jpg", @"green_tea.jpg", @"starbucks_coffee.jpg", @"white_chocolate_donut.jpg", nil];
    for (NSString *imagePath in mainDishImages) {


    NSManagedObjectContext *context = [self managedObjectContext];
    NSManagedObject *newRecipe = [NSEntityDescription insertNewObjectForEntityForName:@"Recipe" inManagedObjectContext:context];
        [newRecipe setValue:imagePath forKey:@"imageFilePath"];
    }
    for (NSString *imagePath in drinkDessertImages) {
        NSManagedObjectContext *context = [self managedObjectContext];
        NSManagedObject *newRecipe = [NSEntityDescription insertNewObjectForEntityForName:@"Deserts" inManagedObjectContext:context];
        [newRecipe setValue:imagePath forKey:@"imageFilePath"];
    }
}

但我也允许用户通过使用设备相机拍照来添加项目。如何保存图像以便我的应用可以再次使用它,并将其集成到我现有的核心数据方案中?

谢谢

【问题讨论】:

  • 您需要全分辨率图像吗?例如,在我正在开发的应用程序中,我需要图像,但我不需要更高的分辨率,因此我将它们作为压缩图像 (jpeg) 存储在 Core Data 中。它运作良好,我不必处理外部参考。如果您必须拥有完整的分辨率,那么您几乎需要将其存储在文件系统中并对其进行引用。
  • 优先保存完整分辨率,因为该应用将允许用户稍后将图像上传到 facebook,并且还会调整图像大小以在 UICollectionView 和详细视图中使用。

标签: ios objective-c image uiimagepickercontroller save


【解决方案1】:

只需将新图像保存到用户文档文件夹并将路径存储在核心数据中。使用 UUID 作为名称存储图像,使其独一无二。

当你来获取core data引用的图片时,尝试使用imageNamed加载图片,如果返回nil尝试加载图片的全路径。


关于UUIDs,请参阅this answer

有关文档文件夹的信息,请参见this answer

【讨论】:

  • 谢谢,如何将内容保存到用户文档文件夹,什么是 UUID?
【解决方案2】:

一旦您将图像下载为数据,请使用此方法将该图像存储在您的文档目录中。下载完成每个图像后,将文件路径(代码块中提到的thumbNailAppFile)更新到您的实体中。希望这会帮助你。

 -(void *)writeDataAsFile:(NSData *)imageData
    {

        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString * documentsDirectory = [paths objectAtIndex:0];
        NSString * thumbNailFilename = [NSString stringWithFormat:@"%@%@.png",@"SomePrefixforimage",[self createUUID]];
        NSString * thumbNailAppFile = [documentsDirectory stringByAppendingPathComponent:thumbNailFilename];  // Image local path 

        [imageData writeToFile:thumbNailAppFile atomically:YES];

    }



- (NSString *)createUUID // Create Unique id for image name.
{
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return [(NSString *)string autorelease];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多