【问题标题】:XCode - Display/delete files from NSDocumentDirectoryXCode - 从 NSDocumentDirectory 显示/删除文件
【发布时间】:2011-11-30 08:28:39
【问题描述】:

我的应用程序有一个带有这些代码的“浏览”按钮,允许用户浏览 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];
}

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

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

【问题讨论】:

    标签: objective-c ios xcode ipad nsdocumentdirectory


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-10-19
      • 2011-04-15
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 2012-02-11
      相关资源
      最近更新 更多