2011-10-04 44 views
4

我的应用程序有一个“浏览”按钮,这些代码允许用户浏览iPad的照片库,选择一张照片并使用NSDocumentDirectory将其存储到应用程序中。XCode - 从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中删除。

可以做到这一点吗?如果是这样,请告诉我如何去做,并分享一些示例代码?我很迷茫,因为我对编程还很陌生。

回答