1

I am using core data to save the filenames for a set of images included in my application.

 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"];
    }
}

But I also allow the user to add items by taking a picture with the device camera. How can I save the image so my app can use it again, and integrate it to work with my existing core data scheme?

Thanks

Andrewp97
  • 15
  • 3
  • Do you need the full resolution image? For example, in the app I'm working on I need the images, but I don't need higher resolution, so I store them in Core Data as a compressed image (jpeg). It works well and I don't have to deal with external references. If you have to have the full resolution, then you pretty much need to store it in the file system with a reference to it. – RegularExpression Jun 28 '13 at 00:06
  • Storing the full resolution would be preferential, because the app will allow users to upload the immage to facebook later, and will also resize the image for use in the UICollectionView and detail view. – Andrewp97 Jun 28 '13 at 00:25

2 Answers2

2

Just save the new images to the users documents folder and store the path in core data. Store the image with a UUID for the name so its unique.

When you come to get the image referenced by core data, try to load the image using imageNamed, if it returns nil try to load the image at the full path.


See this answer about UUIDs.

See this answer about the documents folder.

Community
  • 1
  • 1
Wain
  • 118,658
  • 15
  • 128
  • 151
1

Once you download the image as data use this method to store that image in your document directory. Update the file path (thumbNailAppFile mention in code block) in to your entity once download get over for each image. Hope this will help you.

 -(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];
}
Ganapathy
  • 4,594
  • 5
  • 23
  • 41