Lloydworth
Lloydworth

Reputation: 763

Storing image using NSDocumentDirectory not working?

I'm developing this application for an iPad. My first screen will show 2 UIImageView (ImageView, ImageView2) and 2 UIButton (Confirm, Browse).

I will give a brief description of what my application does. When the 'Browse' button is tapped, it will show photos from my iPad's photo gallery. When a photo is selected, the photo gallery will be dismissed and 'ImageView' will show the selected photo. When the 'Confirm' button is tapped, it will store the image into my application's project by using the NSDocumentDirectory. After that, it will retrieve the photo which is saved and display it on 'ImageView2'.

Browse Button..

- (IBAction) browsePhoto:(id)sender
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
[popover setPopoverContentSize:CGSizeMake(320,320)];
[popover presentPopoverFromRect:CGRectMake(200,200,-100,-100) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popoverController = popover;
[imagePickerController  release];
}

Displaying selected image on ImageView..

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo 
{
[self.popoverController dismissPopoverAnimated:YES];
imageView.image = selectedImage;
}

Confirm Button..

- (IBAction) confirmPhoto:(id)sender
{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:@"SavedImage.png"];
UIImage *image = imageView.image;
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
imageView2.image = [UIImage imageNamed:@"SavedImage.png"];
}

I'm able to view photos from the photo gallery, select a photo, and making the 'ImageView' show the selected photo. But when i tap on 'Confirm' button, 'ImageView2' doesn't display the photo. I have no idea where the problem lies at.

Is the photo even saved? Or the way i used to retrieve the saved photo is wrong?

Upvotes: 0

Views: 1424

Answers (2)

stack2012
stack2012

Reputation: 2186

NSString *filePath=[NSString stringWithFormat:@"%@/%@",documentsDir,@"SavedImage.png"];
imageView2.image=[UIImage imageWithContentsOfFile:filePath];

Upvotes: 1

Nevin
Nevin

Reputation: 7819

Try to load the image with:

[UIImage imageWithContentsOfFile:imageFilePath]

Upvotes: 0

Related Questions