【问题标题】:UICollectionView show images stored in documents directoryUICollectionView 显示存储在文档目录中的图像
【发布时间】:2014-08-19 08:30:45
【问题描述】:

您好,我可以获取UITableView 来显示存储在文档文件夹中的图像的文件名。有没有办法让UICollectionView 在集合视图中显示这些图像?

谢谢利亚姆

【问题讨论】:

  • 是的,有很多教程。

标签: ios objective-c uicollectionview nsdocumentdirectory


【解决方案1】:
    // fetch all the files from the documents directory.
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    library = [path objectAtIndex:0];
    fileArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:library error:nil];

    NSArray *imagelist = [[NSArray alloc] init];

    // filter the images from all the files.
    for(int i = 0;i<fileArray.count;i++)
   {
      id object = [fileArray  objectAtIndex:i];
      if ([object rangeOfString:@".png"].location !=NSNotFound)
     {
       [imagelist addObject:object];

     }
  }

然后创建一个集合视图的实例并使用以下 UICollectionView 数据源将项目添加到集合视图中,

  • (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

【讨论】:

    【解决方案2】:

    是的,你可以。这是UICollectionView Programming Guide

    1. 创建文件名数组
    2. 将数据源连接到UICollectionView
    3. collectionView:cellForRowAtIndexPath: 中使用+imageNamed: 方法返回带有图像的单元格。

    记得创建带有UIImageView 的自定义单元格,创建出口。

    这里是Tutorial,应该可以帮到你。

    【讨论】:

      猜你喜欢
      • 2013-02-24
      • 2014-09-14
      • 2013-12-02
      • 1970-01-01
      • 2015-08-27
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      相关资源
      最近更新 更多