2016-02-15 63 views
0
#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; 
} 

圖像再次從服務器加載並使滾動向上或向下非常滯後。以下是發生這種情況的日誌。UICollectionView圖像一次又一次地在滾動中滯後加載

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 

如何解決滯後問題?應該只從服務器加載一次圖像並保存在緩存或本地。

+0

我重寫了代碼。更改'[imageButton setImage:img forState:UIControlStateNormal];'到'[imageButton setImageWithURL:url forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@「logo.png」]];'。但我得到這個錯誤***終止應用程序由於未捕獲異常'NSInvalidArgumentException',原因:' - [UIButton setImageWithURL:forState:placeholderImage:]:無法識別的選擇器發送到實例0x155ecf00''。 – raymondkwan

回答

0

如果你希望緩存圖像有一些偉大的和流行的第三方框架可用,您可以使用這樣的:

他們可以緩存ANS解決滯後問題

SDWebImageAFNetworking 和你可以在github上找到一些其他更好的框架

如果你想手動維護它,你可以使用'NSCache',你可以在這裏找到一些很好的教程net

+0

謝謝。我有一個'imgarray',它包含代碼中的圖像url數組。我該如何重寫? – raymondkwan

+0

可以遍歷在cellForItemAtIndexPath方法的圖像的陣列與您選擇像 爲SDWebImage ImageView的方法就會像 '代碼 NSURL * imgUrl的= [NSURL URLWithString:[imgarray objectAtIndex:indexPath.row]]; [ImageButton的sd_setImageWithURL:iurl placeholderImage:[UIImage的imageNamed:@ 「」]];' 對於按鈕: '代碼[ImageButton的sd_setImageWithURL:imgUrl的forState:UIControlStateNormal placeholderImage:[UIImage的imageNamed:@ 「」]]; ' 或者如果您在單個索引處有陣列您可以檢查Sharavan的回覆 – HardikDG

+0

Thx。我重寫了代碼。更改'[imageButton setImage:img forState:UIControlStateNormal];'到'[imageButton setImageWithURL:url forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@「logo.png」]];'。但我得到這個錯誤***終止應用程序由於未捕獲異常'NSInvalidArgumentException',原因:' - [UIButton setImageWithURL:forState:placeholderImage:]:無法識別的選擇器發送到實例0x155ecf00''。 – raymondkwan

1
  • 您可以使用SDWebimage來解決問題。
  • 另一種方式是使用dispatch_async/NS操作隊列。
  • 但是,SDWebImage確實在內部使用上述方法之一。就存儲圖像而言,建議使用它。
  • U需要緩存圖像。
+0

@Pyro謝謝!有沒有教程如何重寫代碼?我已經下載了SDWebimage框架並添加了#import行「UIImageView + Webcache」。ħ 到myviewcontroller – raymondkwan

+0

類'#進口' 接着 '[self.Image sd_setImageWithURL:iurl placeholderImage:[UIImage的imageNamed:@ 「」]];' – Shravan

+0

噢THX,但我有一個'imgarray'在代碼中包含圖像url數組。我應該如何重寫? – raymondkwan

相關問題