ios - UICollectionView 图像一次又一次加载,滚动滞后

标签 ios objective-c xcode uicollectionview

#pragma mark Collection View
-(NSInteger )numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [array count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    NSLog(@"indexpath row : %d", indexPath.row);

    UILabel *label = (UILabel *)[cell viewWithTag:100];
    NSString *checkisfolder=[checkfolder objectAtIndex:indexPath.row];
    NSString *selectorValue=[list2 objectAtIndex:indexPath.row];

    NSURL *url = [NSURL URLWithString:[imgarray objectAtIndex:indexPath.row]];
    NSLog(@"indexpath image : %@", [imgarray objectAtIndex:indexPath.row]);
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];

    // load image from JSON URL
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
                       UIImageView *imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 140, 80)] autorelease];

                       if (imgView != nil) {
                           NSLog(@"selectorValue1  : %@", selectorValue);

                           UIImage *img = [[UIImage alloc] initWithData:data];

                           dispatch_async(dispatch_get_main_queue(), ^{
                               UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
                               imageButton.frame = CGRectMake(15, 10, 257, 140);
                               [imageButton setImage:img forState:UIControlStateNormal];

                               imageButton.tag=[selectorValue intValue];

                               imageButton.adjustsImageWhenHighlighted = NO;
                               if([checkisfolder  isEqual: @"isfolder"]){
                                   [imageButton addTarget:self action:@selector(imagefolderClicked:) forControlEvents:UIControlEventTouchUpInside];
                               }else{
                                   [imageButton addTarget:self action:@selector(imageViewClicked:) forControlEvents:UIControlEventTouchUpInside];
                               }
                               [cell addSubview:imageButton];

                               UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(userLongPressed:)];

                               press.minimumPressDuration = 1.0;

                               [imageButton addGestureRecognizer:press];
                               [press release];
                           });
                       };
                   });


    label.text = [array objectAtIndex:indexPath.row];
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.numberOfLines = 0;

    [cell.layer setBorderWidth:1.0f];
    [cell.layer setBorderColor:[UIColor whiteColor].CGColor];
    [cell.layer setCornerRadius:5.0f];

    return cell;
}

图像从服务器重新加载,向上或向下滚动非常滞后。以下是发生这种情况时的日志。

2016-02-15 10:53:55.273 Go Test[2086:651177] indexpath row : 0
2016-02-15 10:53:55.274 Go Test[2086:651177] indexpath image : http://media.movideo.com/media-images-670/media/1358499/320x160.png
2016-02-15 10:53:55.465 Go Test[2086:651298] selectorValue1  : 475
2016-02-15 10:53:55.476 Go Test[2086:651177] indexpath row : 1
2016-02-15 10:53:55.476 Go Test[2086:651177] indexpath image : http://media.movideo.com/media-images-670/media/1358500/320x160.png
2016-02-15 10:53:55.534 Go Test[2086:651284] selectorValue1  : 476
2016-02-15 10:53:55.541 Go Test[2086:651177] indexpath row : 2
2016-02-15 10:53:55.542 Go Test[2086:651177] indexpath image : http://media.movideo.com/media-images-670/media/1358318/320x160.png
2016-02-15 10:53:55.709 Go Test[2086:651283] selectorValue1  : 477
2016-02-15 10:53:55.818 Go Test[2086:651177] there are 0 objects in the array
2016-02-15 10:53:55.819 Go Test[2086:651177] there are 0 objects in the array

如何解决延迟问题?图像应该只从服务器加载一次并保存在缓存或本地。

最佳答案

  • 可以使用SDWebimage解决问题。
  • 另一种方法是使用 dispatch_async/NS 操作队列。
  • 但是 SDWebImage 确实在内部使用了上述方法之一。就存储图像而言,建议使用。
  • 你需要缓存图片。

关于ios - UICollectionView 图像一次又一次加载,滚动滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35401073/

相关文章:

objective-c - MFSideMenuController 内部 viewwillappear 方法不起作用

ios - 有没有办法知道何时输入了某个 UITextView?

ios - 使用委托(delegate)和 UITabBarController 转发数据

ios - 错误的 Swift 类型转换尝试到完全不同的对象类型

ios - 在 2 个 View Controller 之间传递一个字符串

ios - 在 IOS 5 上将应用程序限制为特定方向

ios - 这个 Objective-C/XCode 注释是什么//[START_EXCLUDE]

ios - 无法将条形按钮项目添加到 Xcode 中的导航 Controller

ios - 在没有许可和 ERROR ITMS-90121 之间提交的循环错误

ios - 在 Swift 中使用参数将图像发送到服务器