5

我正在 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 的缩略图/表格视图中的所有照片。

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

我怎样才能做到这一点?

4

1 回答 1

2

首先,在将图像存储在 Documents 文件夹中时,尝试使用命名约定来存储它们,例如保留一个计数器变量并根据它为图像命名。像这样的一些事情:

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

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

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

我希望这可以帮助你。

要使用图像填充表格,您需要一个自定义单元格。您可以参考 Apple 的一个名为AdvancedTableViewCells的教程。它将向您展示如何使用图像填充表格。他们在每一行都有一个主缩略图。您必须根据您的要求对其进行自定义并使其成为 3 或 4 个缩略图。除此之外,从该自定义单元格中删除所有内容。

于 2011-10-04T06:20:24.820 回答