How to display all images from NSDocument directory

后端 未结 3 612
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 15:51

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

i am using this code to

3条回答
  •  梦谈多话
    2020-12-30 16:32

    This answer is about how to retrieve images from documents directory and displaying them to 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.....

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

提交回复
热议问题