ios UICollectionView 加载数据后 滑动卡顿问题

最近项目的资源图片变大了,滑动时总是卡顿,在这里用NSOperationQueue解决了一下

.h 文件
@interface CollectionViewCell : UICollectionViewCell
// 赋值
@property (nonatomic, strong) NearRentListModel *listModel;
// 队列
@property (nonatomic, strong) NSOperationQueue *queue;

.m 文件
// 懒加载
- (NSOperationQueue *)queue {
    
    if (!_queue) {
        _queue = ({
            NSOperationQueue *q = [[NSOperationQueue alloc]init];
            //设置最大并行操作数为1相当于将queue设置为串行线程
            q.maxConcurrentOperationCount = 1;
            q;
        });
    }
    return _queue;
}
// 调用set方法
- (void)setListModel:(NearRentListModel *)listModel {
    
    _listModel = listModel;
   // 这里展示其他数据(根据项目需要而定)
      
// 开始使用,解决卡顿
if (self.queue.operationCount >= 2) {
        //如果self.queue.operations[0]正在执行的话,不会被强行中止
        [self.queue cancelAllOperations];
    }
    NSInvocationOperation *operation = [[NSInvocationOperation alloc]
                                        initWithTarget:self
                                        selector:@selector(loadImg)
                                        object:nil];
    
    [self.queue addOperation:operation];
    
}

// 调用 展示网络图片
- (void)loadImg{
    dispatch_async(dispatch_get_main_queue(), ^{
         [_headerImg sd_setImageWithURL:[NSURL URLWithString:_listModel.avatar_path] placeholderImage:MoTo_User_headerimage];
        
        [_img sd_setImageWithURL:[NSURL URLWithString:_listModel.cover_photo] placeholderImage:nil];
    });
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值