objective-c – 显示NSDocumentDirectory中的照片

objective-c – 显示NSDocumentDirectory中的照片,第1张

概述我正在iPad上开发这个应用程序. 这些“浏览”按钮代码允许用户从iPad的图库中查看照片. - (IBAction) BrowsePhoto:(id)sender{UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];imagePickerController.dele 我正在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个缩略图.除此之外,从该自定义单元格中删除所有内容

总结

以上是内存溢出为你收集整理的objective-c – 显示NSDocumentDirectory中的照片全部内容,希望文章能够帮你解决objective-c – 显示NSDocumentDirectory中的照片所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1045587.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存