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

发布于 2024-12-08 06:46:08 字数 1538 浏览 0 评论 0原文

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

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

My application has a 'Browse' button with these codes that allows user to browse the iPad's photo gallery, select a photo and store it into the application using 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];
}

Now i want to include a 'Display' button which displays all the photos from the NSDocumentDirectory in a new view. Was thinking of displaying it in thumbnails and also when an image is tapped, it will have a pop up asking the user to confirm if he/she wants to delete the selected photo. If yes, the photo will be removed from the NSDocumentDirectory.

Is it possible to do this? If it is, mind telling me how to do it and share some sample codes? I'm quite lost as i'm still quite new to programming.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心旳酸楚 2024-12-15 06:46:08

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

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

Everything you need to do with files is implemented in system class called NSFileManager.

It's not complicated. Try to read the documentation and google some examples. There is a lot of them, e.g. http://www.theappcodeblog.com/2011/04/04/nsfilemanager-tutorial-create-read-and-delete-a-file/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文