Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying photos from NSDocumentDirectory

I'm developing this application on an iPad.

These codes for a 'Browse' button allows the user to view the photos from the iPad's gallery.

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

When a photo is selected, it will be stored into the application using NSDocumentDirectory.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo 
{
[self.popoverController dismissPopoverAnimated:YES];
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];
}

Now i have to include a 'Display' button on the first screen. When tapped on the button, it will show a new view (modal view controller) and displays all the photos in thumbnails/tableview from the NSDocumentDirectory.

When a photo is selected, it will be deleted from the NSDocumentDirectory.

How can i do this?

like image 328
Lloydworth Avatar asked Nov 05 '22 13:11

Lloydworth


1 Answers

First of all, while storing the image in Documents folder, try storing them with a naming convention like keep a counter variable and give your images names according to it. Some thing like this:

NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", counter]];

Now, once you've all images stored in documents folder and wish to get all of them you can get them by writing this code:

 -(NSMutableArray *)GetImage:(NSMutableArray *)arrayImgNames
 {
      NSMutableArray *tempArray;
            for(int i=0;i<[arrayImgNames count]; i++)
     {
        NSArray *paths1 = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths1 objectAtIndex:0];
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent: [arrayImgNames objectAtIndex:i]];
        [tempArray addObject:[[UIImage alloc] initWithContentsOfFile:filePath]];
        return tempArray;
     }
 }

Once you've got all images you can display them as thumbnails and then when you wish to delete an image use this method:

 -(int)removeImage:(NSString*)FileName1
 {
     NSFileManager *filemanager=[NSFileManager defaultManager]; 
     NSArray *path1=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);  
     NSString *documentdirectory = [path1 objectAtIndex:0]; 
     NSString *filepath=[documentdirectory  stringByAppendingPathComponent:FileName1];
     if([filemanager fileExistsAtPath:filepath]==TRUE)
     {
        [filemanager removeItemAtPath:filepath error:nil];
     }  
     return 0;
 }

I hope this helps you.

To Populate the table with image you'll need a custom cell. You can refer a tutorial by Apple called AdvancedTableViewCells. It will show you how can you populate your table with images. They have a main thumbnail in every row. You have to customize it and make it 3 or 4 thumbnails as per your requirement. Other than that remove everything from that custom cell.

like image 92
Dip Dhingani Avatar answered Nov 15 '22 06:11

Dip Dhingani