Llo*_*rth 5 xcode objective-c ipad ios nsdocumentdirectory
我正在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];
}
Run Code Online (Sandbox Code Playgroud)
选择照片后,将使用NSDocumentDirectory将其存储到应用程序中.
- (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];
}
Run Code Online (Sandbox Code Playgroud)
现在我必须在第一个屏幕上包含一个"显示"按钮.点击按钮时,它将显示一个新视图(模态视图控制器),并从NSDocumentDirectory显示缩略图/ tableview中的所有照片.
选择照片后,它将从NSDocumentDirectory中删除.
我怎样才能做到这一点?
首先,在将图像存储在“文档”文件夹中时,尝试使用命名约定来存储它们,例如保留计数器变量并根据它为图像命名。像这样的事情:
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", counter]];
Run Code Online (Sandbox Code Playgroud)
现在,一旦您将所有图像存储在文档文件夹中并希望获取所有图像,您可以通过编写以下代码来获取它们:
-(NSMutableArray *)GetImage:(NSMutableArray *)arrayImgNames
{
NSMutableArray *tempArray;
for(int i=0;i<[arrayImgNames count]; i++)
{
NSArray *paths1 = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths1 objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent: [arrayImgNames objectAtIndex:i]];
[tempArray addObject:[[UIImage alloc] initWithContentsOfFile:filePath]];
return tempArray;
}
}
Run Code Online (Sandbox Code Playgroud)
获得所有图像后,您可以将它们显示为缩略图,然后当您想要删除图像时,请使用以下方法:
-(int)removeImage:(NSString*)FileName1
{
NSFileManager *filemanager=[NSFileManager defaultManager];
NSArray *path1=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentdirectory = [path1 objectAtIndex:0];
NSString *filepath=[documentdirectory stringByAppendingPathComponent:FileName1];
if([filemanager fileExistsAtPath:filepath]==TRUE)
{
[filemanager removeItemAtPath:filepath error:nil];
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助你。
要使用图像填充表格,您需要一个自定义单元格。您可以参考 Apple 提供的名为AdvancedTableViewCells的教程。它将向您展示如何使用图像填充表格。每行都有一个主缩略图。您必须根据您的要求对其进行自定义并制作 3 或 4 个缩略图。除此之外,删除该自定义单元中的所有内容。