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