首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示来自NSDocumentDirectory的照片

显示来自NSDocumentDirectory的照片
EN

Stack Overflow用户
提问于 2011-10-04 13:39:18
回答 1查看 1.9K关注 0票数 5

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

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

代码语言:javascript
复制
- (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存储到应用程序中。

代码语言:javascript
复制
- (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中删除。

我该怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-10-04 14:20:25

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

代码语言:javascript
复制
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", counter]];

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

代码语言:javascript
复制
 -(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;
     }
 }

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

代码语言:javascript
复制
 -(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个缩略图。除此之外,从该自定义单元格中删除所有内容。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7643713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档