如何显示NSDocumentDirectory中的照片

时间:2011-10-04 23:49:43

标签: iphone ios

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

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

代码:

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

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

Code:
- (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显示缩略图/ tableview中的所有照片。

选择照片后,它将从NSDocumentDirectory中删除。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

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

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