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