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

时间:2011-10-04 09:54:31

标签: objective-c ios xcode ipad nsdocumentdirectory

目前我正在使用这些代码将我的图像保存到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中检索所有图像,以便我可以在另一个视图中显示它们。以下代码显示了我如何检索它们。

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

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

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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

从NSDocumentDirectory中检索所有图像并存储到uiimageview viewcontroller.m

  • (无效)viewDidLoad中 { [super viewDidLoad];

    for (int i=0; i<5; i++) {
    
    imgview=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    
    NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
    

    NSString * docDirectory = [sysPaths objectAtIndex:0];

    NSString *str=[NSString stringWithFormat:@"img%d.jpg",i];
    
    NSLog(@"abc %@",str);
    

    NSString * imagePath = [docDirectory stringByAppendingPathComponent:str];

    imgview.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:imagePath]];
    [self.view addSubview:imgview]; [imgview setUserInteractionEnabled:YES];

    x=x+width+margin;
    if (x>=totalwidth) {
    
        y=y+height+margin;
        x=margin;
    
    }    
    

    } }