objective-c - XCode - 显示/删除 NSDocumentDirectory 中的文件

标签 objective-c ios xcode ipad nsdocumentdirectory

我的应用程序有一个带有这些代码的“浏览”按钮,允许用户浏览 iPad 的照片库,选择一张照片并使用 NSDocumentDirectory 将其存储到应用程序中。

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

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

现在我想包含一个“显示”按钮,它可以在新 View 中显示 NSDocumentDirectory 中的所有照片。正在考虑以缩略图显示它,并且当点击图像时,它会弹出一个窗口,要求用户确认他/她是否要删除所选照片。如果是,照片将从 NSDocumentDirectory 中删除。

这有可能吗?如果是,请介意告诉我该怎么做并分享一些示例代码?我很迷茫,因为我对编程还很陌生。

最佳答案

您需要对文件执行的所有操作都在名为 NSFileManager 的系统类中实现。

并不复杂。尝试阅读文档并用谷歌搜索一些示例。它们有很多,例如http://www.theappcodeblog.com/2011/04/04/nsfilemanager-tutorial-create-read-and-delete-a-file/

关于objective-c - XCode - 显示/删除 NSDocumentDirectory 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7642610/

相关文章:

objective-c - 保存文件避免重复文件名和覆盖

objective-c - NSDateFormatter在iOS 5.1和iOS 6中的行为

ios - 方法未在协议(protocol)中实现(使用 wit.ai SDK)

ios - 从详细 View Controller ios 发送回信息

ios - pageViewController内容跳转

ios - 如何在相机拍摄后将图像保存到应用程序的内存中

ios - VERBOSE-2 : Platform_view_layer. cc(28) - 尝试嵌入平台 View 但 PaintContext 不支持嵌入

swift - 使用防护/if 语句来设置多个 CollectionvViewCell 样式的良好做法?

xcode - 遵循 Apple 的应用程序图标指南不会为 macOS 应用程序图标提供圆角半径

iphone - UIImagePickerController - 从应用程序文档目录保存和检索照片