首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从url加载UIImage的IOS UIImage

从url加载UIImage的IOS UIImage
EN

Stack Overflow用户
提问于 2014-12-07 17:42:47
回答 3查看 5.4K关注 0票数 1

我有一个UICollectionView,我想把图片从网络上加载到UIImageView上。(单元格上带有标记:100的UIImageView。)

当我使用这种方法时,图片不会出现:

代码语言:javascript
复制
- (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;
}

但是当我在项目中使用图片时,它可以正确地显示出来。就像这样:

代码语言:javascript
复制
 -(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;
}

为什么当我使用来自网络的图片加载时,它不能工作?(图片可以通过“浏览器”打开。)

能给我一些提示或者正确的代码吗?非常感谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-12-07 19:44:01

您期望从这一行获得一个UIImageView:

代码语言:javascript
复制
UIImageView *chImage = (UIImageView *)[cell viewWithTag:100];

但你不是。你得到的只是一个UIView。因此,您必须创建一个UIImageView,然后使用addSubview:方法将这个UIImageView添加到backgroudView视图中。

因此,应该是:

代码语言:javascript
复制
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获取图像是一个很棒的特性。

票数 3
EN

Stack Overflow用户

发布于 2014-12-08 10:19:01

通过子类ImageCell创建类UICollectioViewCell,并创建一个属性ImageCell.h:

代码语言:javascript
复制
@interface ImageCell : UICollectionViewCell
    @property (nonatomic, strong) UIImage *dImage;
@end

然后在ImageCell.m中添加一个函数

代码语言:javascript
复制
- (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委托:

代码语言:javascript
复制
-(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。如果你有任何困惑请告诉我。

票数 2
EN

Stack Overflow用户

发布于 2015-03-22 16:29:39

只需将数据获取部分从集合视图的委托移动到它的viewDidLoad。

将图像存储到数组中。并将其用作集合视图的数据源。

将图像添加到数组中的ie.Each时间,只需重新加载集合视图。

或者,您可以创建一个自定义单元格,按照“iOSGeek”的建议执行此操作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27345681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档