【问题标题】:Displaying photos from NSDocumentDirectory显示来自 NSDocumentDirectory 的照片
【发布时间】:2011-11-30 10:02:24
【问题描述】:

我正在 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 中删除。

我该怎么做?

【问题讨论】:

    标签: objective-c ios xcode ipad nsdocumentdirectory


    【解决方案1】:

    首先,在将图像存储在 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 个缩略图。除此之外,从该自定义单元格中删除所有内容。

    【讨论】:

    • 谢谢!但是您知道如何将图像数组显示到带有缩略图的新视图中吗?
    • 请一张桌子。使其自定义单元格在一行中包含 4 个图像中的 3 个,以方便您。从我们拥有的图像数组中填充图像,您将显示所有图像。您还需要为自定义单元格中的每个图像(每个图像右上角的一个小十字)设置一个按钮来删除图像。
    • 不介意举例说明如何用数组填充表格?
    • 我编辑了我的答案并提到了一个教程,您可以参考该教程来使用数组填充表格。
    • 非常感谢,救世主。这对我会有很大的帮助。但我有一个问题,如果我不使用计数器作为命名约定保存它们,是否可以检索所有图像?
    猜你喜欢
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    相关资源
    最近更新 更多