0

I want to know that how to retrieve image from image gallery and save the path of it in local database. After this process I want that I have used images from my local database image path. Please help!

Thanks.

Saurabh
  • 423
  • 4
  • 11
  • 22
  • the devil is always in the details. so help us out. what kind of database? – FluffulousChimp Sep 19 '12 at 10:34
  • Sorry... I am using sqlite database. And I want that I have store the path of image in it so that in future I'll get image from this path. – Saurabh Sep 19 '12 at 10:36
  • Be more specific .... There are more than one question. Well you can start from here http://stackoverflow.com/questions/11580918/nsblockoperation-or-nsoperation-with-alasset-block-to-display-photo-library-imag to get all images URL from iPhone gallery .. – TheTiger Sep 19 '12 at 10:41

1 Answers1

0

About sqlite and images

Storing paths (as opposed to images) in sqlite makes sense. Or you could consider using Core Data. Now that Core Data provides the ability to store images or other large data in external storage (essentially abstracting away the need to find your own directory, get the path, store the path, etc.) I would favor it.

The code below assumes you can deal with the messy details of sqlite or are using some library as a wrapper around the raw implementation.

Storing images locally

You can decide where you want to store the images in your app's file system. Let's assume you want to use the documents directory:

- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

Let's also assume these are png format for illustrative purposes:

- (NSString *)pathForImageNamed:(NSString *)imageName {
    return [[self applicationDocumentsDirectory] stringByPathComponent:imageName];
}

- (void)saveImage:(UIImage *)image name:(NSString *)name {
    NSString *path = [self pathForImageNamed:imageName];
    [UIImagePNGRepresentation writeToFile:path];

    // here, save path to sqlite
}

Retrieving images

- (UIImage *)imageWithName:(NSString *)imageName {
    NSString *imagePath = nil;

    // retrieve path name from sqlite db based on image name

    return [UIImage imageWithContentsOfFile:imagePath];
}
FluffulousChimp
  • 9,157
  • 3
  • 35
  • 42