显示来自NSDocumentDirectory的照片



我正在iPad上开发这个应用程序。

这些"浏览"按钮的代码允许用户查看iPad图库中的照片。

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

当照片被选中时,它将通过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];
}

现在我必须在第一个屏幕上包含一个"显示"按钮。当点击按钮时,它会显示一个新视图(模态视图控制器)并显示NSDocumentDirectory中thumbnails/tableview中的所有照片。

当照片被选中时,它将从NSDocumentDirectory中删除。

我该怎么做?

首先,当将图像存储在Documents文件夹中时,尝试使用命名约定(如保留计数器变量)存储它们,并根据它为图像命名。像这样:

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

现在,一旦你将所有的图像存储在documents文件夹中,并希望获得所有的图像,你可以通过编写以下代码来获取它们:

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

一旦你有了所有的图片,你可以把它们显示为缩略图,然后当你想删除一个图像时,使用这个方法:

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

我希望这对你有帮助。

要用图像填充表,您需要一个自定义单元格。你可以参考苹果的教程AdvancedTableViewCells。它将向您展示如何用图像填充表。每一行都有一个主缩略图。你必须自定义它,使它3或4个缩略图根据您的要求。除此之外,删除自定义单元格中的所有内容。

最新更新