我有一个UICollectionView,我想把图片从网络上加载到UIImageView上。(单元格上带有标记:100的UIImageView。)
当我使用这种方法时,图片不会出现:
- (void)viewDidLoad
{
[super viewDidLoad];
Img = [NSArray arrayWithObjects:@"http://192.168.1.103:8088/Images/1.png",@"http://192.168.1.103:8088/Images/2.png",nil];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"List";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *chImage = (UIImageView *)[cell viewWithTag:100];
NSURL *imageUrl = [NSURL URLWithString:[Img objectAtIndex:indexPath.row]];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
chlImage.image = image;
NSLog("%@",[Img objectAtIndex:indexPath.row]); //Here can show Img's values correctly
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"backgroung.png"]];
return cell;
}但是当我在项目中使用图片时,它可以正确地显示出来。就像这样:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"List";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *chImage = (UIImageView *)[cell viewWithTag:100];
chImage.image = [UIImages imageNames:@"123.jpg"];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"backgroung.png"]];
return cell;
}为什么当我使用来自网络的图片加载时,它不能工作?(图片可以通过“浏览器”打开。)
能给我一些提示或者正确的代码吗?非常感谢!
发布于 2014-12-07 19:44:01
您期望从这一行获得一个UIImageView:
UIImageView *chImage = (UIImageView *)[cell viewWithTag:100];但你不是。你得到的只是一个UIView。因此,您必须创建一个UIImageView,然后使用addSubview:方法将这个UIImageView添加到backgroudView视图中。
因此,应该是:
UIImageView *chImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.backgroundView.frame.size.width,cell.backgroundView.frame.size.height)];
NSURL *imageUrl = [NSURL URLWithString:[Img objectAtIndex:indexPath.row]];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
chImage.image = image;
NSLog("%@",[Img objectAtIndex:indexPath.row]); //Here can show Img's values correctly
[cell.backgroundView addSubview:chImage];最后,我有个建议给你。请看一下SDWebImage库。从URL获取图像是一个很棒的特性。
发布于 2014-12-08 10:19:01
通过子类ImageCell创建类UICollectioViewCell,并创建一个属性ImageCell.h:
@interface ImageCell : UICollectionViewCell
@property (nonatomic, strong) UIImage *dImage;
@end然后在ImageCell.m中添加一个函数
- (void)downloadImageFromURL:(NSURL *)imageUrl
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
dispatch_sync(dispatch_get_main_queue(), ^{
UIImageView *chImageView = (UIImageView *)[cell viewWithTag:100];
chImageView.image = dImage;
});
});
}现在来自UICollectionView委托:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"List";
ImageCell *cell = (ImageCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
NSURL *imageUrl = [NSURL URLWithString:[Img objectAtIndex:indexPath.row]];
[cell downloadImageFromURL:imageUrl];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
return cell;
}最后,请确保您将标记100设置为您想要得到的ImageView。如果你有任何困惑请告诉我。
发布于 2015-03-22 16:29:39
只需将数据获取部分从集合视图的委托移动到它的viewDidLoad。
将图像存储到数组中。并将其用作集合视图的数据源。
将图像添加到数组中的ie.Each时间,只需重新加载集合视图。
或者,您可以创建一个自定义单元格,按照“iOSGeek”的建议执行此操作。
https://stackoverflow.com/questions/27345681
复制相似问题