gpt4 book ai didi

iphone - 如何显示 NSDocumentDirectory 中的照片

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:25:28 24 4
gpt4 key购买 nike

我正在 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];
}

现在我必须在第一个屏幕上包含一个“显示”按钮。点击按钮时,它将显示一个新 View (模态视图 Controller )并在 NSDocumentDirectory 的缩略图/表格 View 中显示所有照片。

当照片被选中时,它会从 NSDocumentDirectory 中删除。

我该怎么做?

最佳答案

要删除 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com