ios - UICollectionView 不显示任何图像

标签 ios objective-c uicollectionview uicollectionviewcell

我试图在 UICollectionView 中显示文档目录中的图像,一切正常,但没有图像出现

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Initialize recipe image array

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];



    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];


    NSMutableArray *image = [NSMutableArray array];


    for (NSString *tString in dirContents) {

        if ([tString hasSuffix:@".png"]) {

            [image addObject:tString];


            listeImages = [NSArray arrayWithArray:image];


        }

    }


}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return listeImages.count;



}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *identifier = @"Cell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];



    UIImageView *listeImageView = (UIImageView *)[cell viewWithTag:100];

    UILabel *LabelView = (UILabel *)[cell viewWithTag:110];

    LabelView.text = [listeImages objectAtIndex:indexPath.row];

    listeImageView.image = [UIImage imageNamed:[listeImages objectAtIndex:indexPath.row]];

   NSLog(@" List UIImageView listeImageView.image  %@",listeImageView.image);   

cell.backgroundColor = [UIColor redColor];

  cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];



     NSLog(@" List NSArray *listeImages %@",listeImages);

    cell.tag = indexPath.row;

    return cell;

}

通过这段代码,可以很好地显示正确的 UICollectionView UILabel,但没有显示图像。

我使用这行代码将我的图像存储在 listeImageView.image 中。

 UIImageView *listeImageView = (UIImageView *)[cell viewWithTag:100];
    listeImageView.image = [UIImage imageNamed:[listeImages objectAtIndex:indexPath.row]];

这是我的 NSLog 的结果:

2014-10-11 09:46:31.117 imgapp[2803:60b]  List NSArray *listeImages;  (
    "image10102014233607.png",
    "image10102014233616.png",
    "image10102014233627.png"
)
2014-10-11 09:46:31.158 imgapp[2803:60b]  List UIImageView listeImageView.image  (null)
2014-10-11 09:46:31.171 imgapp[2803:60b]  List UIImageView listeImageView.image  (null)
2014-10-11 09:46:31.178 imgapp[2803:60b]  List UIImageView listeImageView.image  (null)

我们将不胜感激。

谢谢

最佳答案

您有以下内容,

listeImageView.image = [UIImage imageNamed:[listeImages objectAtIndex:indexPath.row]];

但你的所作所为,

  cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];

为什么你不使用你的 listeImageView 如下:-

cell.backgroundView = listeImageView;

这应该让你得到图像。

更新:-

 for (NSString *tString in dirContents) {

    if ([tString hasSuffix:@".png"]) {

              **[image addObject:[UIImage imageWithContentsOfFile:tString] ];  //Do this as in below line you were not create a image object which you are trying to get.**

       //Also check your image array for object, that is it getting stored or not.
       //     [image addObject:tString];

    }

}

 listeImages = [NSArray arrayWithArray:image];  //Do this after you have added all objects in your image mutable array and not in for loop as this will initialise it many tiems depending on your loop run count.

关于ios - UICollectionView 不显示任何图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26293902/

相关文章:

ios - PDFKit Swift 控制缩放

ios - geofence didEnterRegion/didExitRegion通知后如何进行BG NW下载

ios - 尝试从 NSMutableArray 填充 UITableView 时应用程序崩溃

swift - 如何根据标签文本快速更改 CollectionView 单元格宽度

ios - UICollectionView 与搜索标题和拉动刷新

ios - 屏幕上显示的应用内购买价格(带货币)

ios - CoreData导入重载内存

iOS 谷歌分析实现错误

iphone - 我可以在我的应用程序上禁用主页按钮吗? (全屏浏览器信息亭模式)

ios - 多个 UiCollectionview 重新加载问题