网络获取加载图片的三种方法

来源:互联网 发布:电商数据分析淘宝实战 编辑:程序博客网 时间:2024/05/16 19:51

// 加载项的图片

    NSString *imageUrl = deal.imageFileName;

    

    // 1.同步访问

    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];

    UIImage *image = [UIImage imageWithData:imageData];

    cell.imageDeal.image = image;

    // 2.异步

    NSURL *url = [NSURL URLWithString:imageUrl];

    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

        UIImage *image = [UIImage imageWithData:data];

        cell.imageDeal.image = image;

    }];

    // 3.使用SDWebImage第三方库

    [cell.imageDeal sd_setImageWithURL:[NSURL URLWithString:imageUrl]];


0 0
原创粉丝点击