Reputation: 833
I'm currently working on an app that will allow me to select photos from my photo album to copy into my app for viewing them in a tableView. Basically I have a view that has an add button and a view button. When the add button is pressed the UIIMagePickerController displays showing me the photos.
Once I select a photo I would like to copy this photo to my local app. Once the view button is pressed a tableView will be loaded showing all of the photos that were copied previously. I'm stuck at this portion at the moment. The actual copying and storing of the image for future viewing.
Any suggestions on how to accomplish this? How about creating a plist and add the file there?
Thanks in advance!
T
Upvotes: 2
Views: 305
Reputation: 11127
If you want to save the images from UIIMagePickerController
to your app, then the best way to do it is using your document directory folder, save those images to document folder.
Below i will post some codes which will help you to save the image and retreive them.
-(void) saveImageNamed :(NSString *) imageName andImage :(UIImage *) image{
NSData *pngData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:imageName]; //Add the file name
[pngData writeToFile:filePath atomically:YES];
}
and here is how you retreive image from folder
-(UIImage *) getImageWithPath :(NSString *) imagePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSData *pngData = [NSData dataWithContentsOfFile:[documentsPath stringByAppendingPathComponent:imagePath]];
UIImage *image = [UIImage imageWithData:pngData];
return image;
}
Hope this helps you.
Upvotes: 1