gpt4 book ai didi

ios - UICollectionView 不显示任何图像

转载 作者:行者123 更新时间:2023-11-29 02:36:32 24 4
gpt4 key购买 nike

我试图在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com