【问题标题】:How to display all images from NSDocument directory如何显示 NSDocument 目录中的所有图像
【发布时间】:2012-07-02 20:50:27
【问题描述】:

首先我从照片库中选择图像到 ALAsset 库,然后我将图像从 ALAsset 库路径存储在文档目录中。

我正在使用此代码将图像存储在 ALAsset 库中的文档目录中......它的工作完美......现在我想在表格视图中显示存储在文档目录中的所有图像......我该怎么做??? 谁能帮帮我??

将图像从 ALAsset 库导入到 NSdocument 目录的代码

for (int j=0; j<[assetArray count]; j++) {

ALAssetRepresentation *representation = [[assetArray objectAtIndex:j] defaultRepresentation];
NSString* filename = [documentPath stringByAppendingPathComponent:[representation filename]];

[[NSFileManager defaultManager] createFileAtPath:filename contents:nil attributes:nil];
NSOutputStream *outPutStream = [NSOutputStream outputStreamToFileAtPath:filename append:YES];
[outPutStream open];

long long offset = 0;
long long bytesRead = 0;

NSError *error;
uint8_t * buffer = malloc(131072);
while (offset<[representation size] && [outPutStream hasSpaceAvailable]) {
    bytesRead = [representation getBytes:buffer fromOffset:offset length:131072 error:&error];
    [outPutStream write:buffer maxLength:bytesRead];
    offset = offset+bytesRead;
}
[outPutStream close];
free(buffer);

}

之后我使用此代码获取目录的内容:

 NSFileManager *manager = [NSFileManager defaultManager];
fileList = [manager directoryContentsAtPath:newDir];

它也可以工作...但是现在当我想显示文档目录中的图像时。它没有显示任何东西......

 setImage.image=[UIImage imageNamed:[filePathsArray objectAtIndex:0]];

谁能帮忙,问题出在哪里?????? - 我有一个疑问: *将图像从ALAsset库导入到文档目录是否正确???

【问题讨论】:

  • 如果对您有帮助,请您标记我的答案是否正确!谢谢!!!

标签: iphone objective-c ios5 alassetslibrary nsdocumentdirectory


【解决方案1】:

您可以通过这种方式从文档目录中获取所有图像文件:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
    NSMutableArray *imgFiles = [[NSMutableArray alloc] init];
    for (int i=0; i<filePathsArray.count; i++) {
        NSString *strFilePath = [filePathsArray objectAtIndex:0];
        if ([[strFilePath pathExtension] isEqualToString:@"jpg"]) {
            [imgFiles addObject:[filePathsArray objectAtIndex:i]];
        }
    }

    NSLog(@"array with paths of image files in the Document Directory %@", filePathsArray);

然后您可以像这样将图像显示到UITableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 50, 50)];
    img.image = [UIImage imageWithContentsOfFile:[imgFiles objectAtIndex:indexPath.row]];
    [cell addSubview:img];

    return cell;
}

干杯!!!

【讨论】:

  • 感谢回复我得到了数组中的所有图像。但是当我使用此路径在图像视图中显示图像时,它不显示任何图像...我不知道为什么????
  • 对不起,它没有工作.... imgfiles 有内容,但它没有显示在 UIImageView....
【解决方案2】:

你可以通过这个直接获取内容:

NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] error:NULL];

设置图片:

[imgView setImage:[UIImage imageWithContentsOfFile:"Your complete path"]];

【讨论】:

  • 感谢回复.......我得到了内容,但它没有在图像视图中显示图像......为什么???
  • NSArray *fileList = [manager directoryContentsAtPath:newDir]; setImage.image=[UIImage imageNamed:[fileList objectAtIndex:0]];我正在使用此代码...
  • setImage.image=[UIImage imageWithContentsOfFile:[imgFiles objectAtIndex:indexPath.row]];在最后一行这样做...
  • 如果您在表格视图中使用它,只需将您的代码编辑为:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { [yourImageView setImage :[UIImage imageWithContentsOfFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[fileList objectAtIndex:Indexpath.row]];
  • 简短而简单的解决方案。为我工作:)
【解决方案3】:

这个答案是关于如何从文档目录中检索图像并将它们显示到 UITableView.....

首先,您必须将所有图像从您的文档目录中获取到一个数组中......

-(void)viewWillAppear:(BOOL)animated
{

    arrayOfImages = [[NSMutableArray alloc]init];
NSError *error = nil;


    NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];

    NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath: stringPath  error:&error];

  for(int i=0;i<[filePathsArray count];i++)
  {
      NSString *strFilePath = [filePathsArray objectAtIndex:i];
      if ([[strFilePath pathExtension] isEqualToString:@"jpg"] || [[strFilePath pathExtension] isEqualToString:@"png"] || [[strFilePath pathExtension] isEqualToString:@"PNG"]) 
      {
         NSString *imagePath = [[stringPath stringByAppendingString:@"/"] stringByAppendingString:strFilePath];
         NSData *data = [NSData dataWithContentsOfFile:imagePath];
        if(data)
        {
          UIImage *image = [UIImage imageWithData:data];
          [arrayOfImages addObject:image];
        }
      }

  }

}

之后 - 使用此数组,您可以在 uitableview 单元格中显示图像 记住不要在你的视图上添加表格视图,直到你的数组被填满.....

#pragma mark - UItableViewDelegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [arrayOfImages count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tablView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    //adjust your imageview frame according to you
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0, 0.0, 470.0, 80.0)];

    [imageView setImage:[arrayOfImages objectAtIndex:indexPath.row]];
    [cell.contentView addSubview:imageView];
    return cell;

}

现在运行它会工作..

【讨论】:

  • 在上面的 data=(null)..... 在数据工作之前......但在这个 NSData *data = [NSData dataWithContentsOfFile:strFilePath];它不工作... :(
  • 请 NSLog(@"%@",strFilePath);在 NSString *strFilePath = [filePathsArray objectAtIndex:i] 之后;看看会发生什么.....然后告诉我
  • 它显示 strFilePath=IMG_0034.PNG
  • 好的伙计...我会在另一个问题中问这个...thanx的帮助..以及将来.....
  • @Raptor - 谢谢!实际上stringPathdocumentsDirectory 这是复制粘贴错误。现已更正。
猜你喜欢
  • 2012-06-12
  • 2020-01-21
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多