cell自适应网络图片大小

本文介绍了一个iOS应用中实现图片缓存和加载的方法。通过使用SDWebImage框架,文章详细展示了如何设置图片缓存、加载网络图片,并在图片未缓存时使用默认图片作为占位符的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

-(void)setModel:(SheBeiModel *)model{

    

    // 先从缓存中查找图片

    UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey: [NSString stringWithFormat:@"%@%@",request_Img_url,model.image]];

    

    // 没有找到已下载的图片就使用默认的占位图,当然高度也是默认的高度了,除了高度不固定的文字部分。

    if (!image) {

        image = [UIImage imageNamed:@"erwrw"];

        //  图片不存在,下载图片

        [self downloadImage:[NSString stringWithFormat:@"%@%@",request_Img_url,model.image]];

    }

    else

    {

        self.imaShow.image = image;

        //手动计算cell

        CGFloat imgHeight = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;

        

        [self.imaShow mas_updateConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(self.contentView.mas_top);

            make.left.equalTo(self.contentView.mas_left).offset(10);

            make.right.equalTo(self.contentView.mas_right).offset(-10);

            make.height.mas_equalTo(imgHeight);

            make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);

            

        }];

    }

 

    _currentLab.text = [NSString stringWithFormat:@"图片-%ld",self.currentTag];

    

//

    

}

 

- (void)downloadImage:(NSString*)imageURL

{

    // 利用 SDWebImage 框架提供的功能下载图片

    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:imageURL] options:(SDWebImageDownloaderUseNSURLCache) progress:^(NSInteger receivedSize, NSInteger expectedSize) {

        

    } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {

        [[SDImageCache sharedImageCache] storeImage:image forKey:imageURL toDisk:YES];

        

        dispatch_async(dispatch_get_main_queue(), ^{

            // 回到主线程做操做

            // 请求完成 刷新代码

            [[NSNotificationCenter defaultCenter] postNotificationName:@"reloaddata" object:nil];

            

        });

        

 

        

        

    }];

    

}

 

转载于:https://www.cnblogs.com/fantasy940155655/p/8058979.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值