简体   繁体   English

如何显示NSDocument目录中的所有图像

[英]How to display all images from NSDocument directory

First i selected images from photo library to ALAsset Library and after that i stored images in document directory from ALAsset library path. 首先,我从照片库中选择了图像到ALAsset库,之后我将图像存储在ALAsset库路径的文档目录中。

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??? 我正在使用此代码将图像存储在ALAsset Library的文档目录中....它的工作完美...现在我想在表视图中显示存储在文档目录中的所有图像..我该怎么做? can anybody help me?? 有谁能够帮我??

Code for importing images from ALAsset Library to NSdocument directory 用于将图像从ALAsset Library导入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]];

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??? - 我有一个疑问:*它是从ALAsset库导入图像到文档目录的正确方法吗?

This answer is about how to retrieve images from documents directory and displaying them to UITableView... .. 这个答案是关于如何从文档目录中检索图像并将它们显示到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];
        }
      }

  }

}

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..... 之后 - 使用此数组,您可以在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;

} }

Now run it will work .. 现在运行它会工作..

You can directly get contents by this : 您可以直接获取内容:

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

To set Image : 要设置图像:

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

You can get all the image files from the Document Directory this way: 您可以通过以下方式从文档目录中获取所有图像文件:

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

And then you can display the images into the UITableView like this: 然后你可以将图像显示到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;
}

Cheers!!! 干杯!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM