加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 显示NSDocumentDirectory中的照片

发布时间:2020-12-14 19:12:26 所属栏目:百科 来源:网络整理
导读:我正在iPad上开发这个应用程序. 这些“浏览”按钮代码允许用户从iPad的图库中查看照片. - (IBAction) BrowsePhoto:(id)sender{UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];imagePickerController.delegate =
我正在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显示缩略图/ tableview中的所有照片.

选择照片后,它将从NSDocumentDirectory中删除.

我怎样才能做到这一点?

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读