从文档文件夹中加载图像到集合视图中

时间:2014-02-15 16:38:29

标签: ios objective-c cocoa-touch uicollectionview

我的资源文件夹中有一个我要加载到集合视图中的图像列表

我的图像文件名位于文档目录的NSAarray中。

·H

{
NSArray *fileList;
NSString * searchedImage;
}

的.m

    NSFileManager *filesm = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSArray *getFilelist= [filesm contentsOfDirectoryAtPath:[paths objectAtIndex:0] error:nil];
    BOOL isDirectory;
    for (searchedImage in getFilelist){
        BOOL fileExistsAtPath = [[NSFileManager defaultManager] fileExistsAtPath:searchedImage isDirectory:&isDirectory];
        if (fileExistsAtPath) {
            if (isDirectory)
            {
                //Found Directory
            }
        }
        if ([[searchedImage pathExtension] isEqualToString:@"png"]) {

            //This is Image File with .png Extension
            NSLog(@"directoryContents = %@",searchedImage);


        }
    }


    //count files
    NSFileManager *fm = [NSFileManager defaultManager];
    NSArray *pathsAmount = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    fileList= [fm contentsOfDirectoryAtPath:[pathsAmount objectAtIndex:0] error:nil];
    int filesCount = [fileList count];
    NSLog(@"filesCount:%d", filesCount);


    UINib *cellNib = [UINib nibWithNibName:@"NibCell" bundle:nil];
    [self.appliancesCollectionView registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];


  }
return self;
}

这会在63293446.png

中正确地重新标记我的图片名称appliance63293447.pngappliance63293448.pngappliance63293449.pngviewDidLoad

我想将这些加载到我的集合视图单元格中。目前,在-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath方法

中,它会为我的图像文件名返回一个空值

到目前为止,我已尝试过对此处的研究

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return fileList.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {    
    static NSString *cellIdentifier = @"cvCell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.backgroundView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: [NSString stringWithFormat:@"%@",searchedImage]]];
    NSLog(@"cell Bg image %@",searchedImage); <-----------------//returns null
    return cell;
}

3 个答案:

答案 0 :(得分:0)

试试这段代码

cell.backgroundView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: [NSString stringWithFormat:@"%@",[fileList objectAtIndex:indexPath.row]]]];

确保searchImage不是零。

答案 1 :(得分:0)

图像的文件名在fileList数组中,而不在searchedImage中(创建图像时,此最后一个变量无用)。

NSString *fileName = [fileList objectAtIndex:indexPath.row];
cell.backgroundView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: fileName]];

这里的问题是,在您的代码中,fileList可以包含非图像的文件,因此imageNamed:将失败,并返回nil。您应该过滤fileList数组,并确保它只包含图像。

答案 2 :(得分:0)

首先通过NSMutableArray *fileList;使fileList可变 下一步在块中添加图像路径到fileList

  if ([[searchedImage pathExtension] isEqualToString:@"png"]) {

       //This is Image File with .png Extension
        NSLog(@"directoryContents = %@",searchedImage);
        [fileList addObject:searchedImage]
   }

然后在cellForItemAtIndexPath:设置图片

cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[fileList objectAtIndex:indexPath.row]]];