Objective c 目标C模型-如何在模型类中下载图像?

Objective c 目标C模型-如何在模型类中下载图像?,objective-c,model-view-controller,background,uiimage,Objective C,Model View Controller,Background,Uiimage,我有一个盒子的模型,它是用来下载一个背景线程中的图像,并从这个下载的图像创建一个图像 viewcontroller有一个自定义uicollectioncell,但它只是一个uiimageview;没什么太复杂的 在cellForItemAtIndexPath中,我想使用模型下载的图像分配单元格的imageview 但是,它不太起作用 图像从未出现 如果我将背景图像下载器移动到cellForItemAtIndexPath并更改一些项目,则图像加载良好 但是我想要的是分离ViewContoller和

我有一个盒子的模型,它是用来下载一个背景线程中的图像,并从这个下载的图像创建一个图像

viewcontroller有一个自定义uicollectioncell,但它只是一个uiimageview;没什么太复杂的

cellForItemAtIndexPath
中,我想使用模型下载的图像分配单元格的imageview

但是,它不太起作用

  • 图像从未出现
  • 如果我将背景图像下载器移动到
    cellForItemAtIndexPath
    并更改一些项目,则图像加载良好
  • 但是我想要的是分离ViewContoller和模型;模型应该做繁重的工作,而viewcontroller只是简单地处理显示

    代码如下

    // ViewController: View Did Load
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        if (!self.picturesArray) self.picturesArray = [NSMutableArray arrayWithCapacity:kNumberOfCells];
    
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.backgroundColor = [UIColor clearColor];
    
        for (int i=0; i<kNumberOfCells; i++)
        {
            Box *box = [[Box alloc] init];
            [self.picturesArray addObject:box];
            box = nil;
        }
    }
    
    
    // ViewController : collectionView delegage
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        MyCustomCollectionViewCell *cell = (MyCustomCollectionViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    
        Box *box = self.picturesArray[indexPath.row];
        cell.backgroundColor = box.bgColor;
        cell.imageView.image = box.image;
    
        return cell;
    }
    
    我的问题是:

  • 如何让长方体模型下载图像,然后在我的cellAtIndexPath中使单元格的imageView从下载的长方体模型图像分配其图像
  • 另一个无关的问题

  • 将模型与项目的实际下载分离不是最佳做法吗?但是如果我不打算把它放在视图控制器中,而不是模型中,它会去哪里,你会如何/何时调用它

  • 感谢您现在将模型与其图像文件分离,并使用一个键(模型的图像URL)将图像保存在缓存中,这是更好的方法

    另外,将初始化方法放入MyCustomCollectionViewCell是配置单元格的更好方法

    我建议您使用(或类似的库)下载和缓存图像

    MyCustomCollectionViewCell.h:

    @interface MyCustomCollectionViewCell : UITableViewCell
    
    -(void) initializeWithBox:(Box*)box;
    
    带有SDWebImage的MyCustomCollectionViewCell.m:

    @implementation MyCustomCollectionViewCell
    
    -(void) initializeWithBox:(Box*)box
    {
        [self setBackgroundColor:[box bgColor]];
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:[box kUrlString]] placeholderImage:nil];
    }
    
    MyCustomCollectionViewCell.m不带库:

    @implementation MyCustomCollectionViewCell
    
    -(void) initializeWithBox:(Box*)box
    {
        [self setBackgroundColor:[box bgColor]];
        __weak typeof(self) weakSelf = self;
        NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:[box kUrlString]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            if (data) {
                UIImage *image = [UIImage imageWithData:data];
                if (image) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        typeof(self) strongSelf = weakself;
                        if(strongSelf)
                            [self.imageView setImage:image];
                    });
                }
            }
        }];
        [task resume];
    }
    
    在视图控制器中:

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        MyCustomCollectionViewCell *cell = (MyCustomCollectionViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    
        Box *box = self.picturesArray[indexPath.row];
        [cell initializeWithBox:box]
        return cell;
    }
    

    我希望在没有第三方的情况下完成,但好吧,我会再尝试一次
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        MyCustomCollectionViewCell *cell = (MyCustomCollectionViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    
        Box *box = self.picturesArray[indexPath.row];
        [cell initializeWithBox:box]
        return cell;
    }