如何显示NSDocument目录中的所有图像 [英] How to display all images from NSDocument directory

查看:114
本文介绍了如何显示NSDocument目录中的所有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

First i selected images from photo library to ALAsset Library and after that i stored images in document directory from ALAsset library path.

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

i am using this code to store images in document directory from ALAsset Library.... Its working perfect... Now i want to display all images which are stored in document directory in table view.. how can i do this??? can anybody help me??

将图像从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);

}

之后我得到了使用此代码的目录内容:

After that i got the contents of directory using this code:

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

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

Its also working... but now when i want to display images from document directory. It doesn't show anything....

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

任何人都可以提供帮助,问题出在哪里?????
- 我有一个疑问:
*这是将图像从ALAsset库导入到文档目录的正确方法???

Can anybody help, where is the problem????? - I have one doubt: *Is it the right way to import images from ALAsset Library to document directory???

推荐答案

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

首先,您必须将文档目录中的所有图像都添加到数组....

first of all you have to get all images from your documents directory to an array....

-(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单元格中显示图像
记住不要在你的视图上添加表视图,直到你的数组填满.....

after that - using this array you can show the image in uitableview cell remember dont add the table view on your view untill your array is fill.....

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

}

现在运行它会起作用..

Now run it will work ..

这篇关于如何显示NSDocument目录中的所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆