gpt4 book ai didi

objective-c - 从 NSDocumentDirectory 中检索所有图像并存储到数组中

转载 作者:行者123 更新时间:2023-11-29 11:20:11 25 4
gpt4 key购买 nike

目前我正在使用这些代码将我的图像保存到 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:[NSString stringWithFormat:@"%d.png", counter]];
UIImage *image = imageView.image;
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
}

我使用此方法是因为使用循环更容易检索所有这些内容。我想从 NSDocumentDirectory 中检索所有图像,以便我可以在另一个 View 中显示它们。以下代码显示了我如何检索它们。

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

但是,我不希望使用计数器作为我的图像的命名约定。我想为它们使用专有名称,但如果这样做,我将不得不更改检索所有图像的方法。

除了我提到的这种方法,还有其他方法可以检索所有图像吗?

最佳答案

您可以使用下一种方法检索文件:

NSURL *url = [[AppDelegate sharedAppDelegate] applicationDocumentsDirectory];
NSError *error = nil;
NSArray *properties = [NSArray arrayWithObjects: NSURLLocalizedNameKey, NSURLLocalizedTypeDescriptionKey, nil];

NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:url
includingPropertiesForKeys:properties options:(NSDirectoryEnumerationSkipsPackageDescendants)
error:&error];

files 中将存储文档目录中所有文件的路径。下一个代码将帮助您获得名称:

NSURL *url = [files objectAtIndex:index];
NSString *localizedName = [url lastPathComponent];

关于objective-c - 从 NSDocumentDirectory 中检索所有图像并存储到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7646086/

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