OGeek|极客世界-中国程序员成长平台

标题: iphone - 如何显示来自 NSDocumentDirectory 的照片 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:19
标题: iphone - 如何显示来自 NSDocumentDirectory 的照片

我正在 iPad 上开发此应用程序。

“浏览”按钮的这些代码允许用户查看 iPad 画廊中的照片。

代码:

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

When a photo is selected, it will be stored into the application using NSDocumentDirectory.

Code:
- (void)imagePickerControllerUIImagePickerController *)picker didFinishPickingImageUIImage *)selectedImage editingInfoNSDictionary *)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 (模态视图 Controller )并显示来自 NSDocumentDirectory 的缩略图/表格 View 中的所有照片。

当一张照片被选中时,它将从 NSDocumentDirectory 中删除。

我该怎么做?



Best Answer-推荐答案


要在 iPhone 中删除 NSDocumentDirectory 中的任何文件,你可以使用此代码,

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath2 = [NSString stringWithFormat"%@/%@", docDirectory,@"SavedImage.png"];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL exist =[fileManager fileExistsAtPath:filePath2];
if(exist)
  [fileManager removeItemAtPath:filePath2 error:NULL];

关于iphone - 如何显示来自 NSDocumentDirectory 的照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7655290/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4