2016-06-15 1 views
0

구문 분석에서 이미지를 다운로드하고 그 후에 이미지를 uicollection보기로 표시하고 있지만 컬렉션보기를 스크롤하면 이미지가 중단됩니다. 여기에 내 코드구문 분석에서 이미지를 다운로드하고 UICollection보기에 표시

-(void)viewWillAppear:(BOOL)animated 
{ 
    CGRect screen = [[UIScreen mainScreen] bounds]; 

    CGFloat height = CGRectGetHeight(screen); 

    PFQuery * query = [PFQuery queryWithClassName:@"Static_Wallpaper"]; 
    [query selectKeys:@[@"thumbnail"]]; 
    if (height == 480) { 
     [query selectKeys:@[@"image4s"]]; 
    } 
    else{ 
     [query selectKeys:@[@"image6s"]]; 
    } 

    [query findObjectsInBackgroundWithBlock:^(NSArray * objects, NSError * error){ 
     if (!error) { 
      [imgArry arrayByAddingObjectsFromArray:objects]; 
      imgArry = [[NSMutableArray alloc] initWithArray:objects]; 
      NSLog(@"%lu",(unsigned long)imgArry.count); 
      [cllectionView reloadData]; 

     } 

    }]; 

} 

이며, 여기에 수집보기에서 이미지를 채우는 코드입니다

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
    return imgArry.count; 
} 


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *identifier = @"Cell"; 

    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 
    PFObject *imageObject = [imgArry objectAtIndex:indexPath.row]; 
    PFFile *imageFile = [imageObject objectForKey:@"image4s"]; 
    [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) { 
     if (!error){ 
      NSLog(@"is there any data? %@", data); 
      cell.imgView.image = [UIImage imageWithData:data]; 
      ; 

     } 
     else { 
      NSLog(@"no data!"); 
     } 
    }]; 



    return cell; 
} 

답변

1

SDWebimage 당신은 다음과 같은 코드를 수행해야합니다

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *identifier = @"Cell"; 

    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 
    PFObject *imageObject = [imgArry objectAtIndex:indexPath.row]; 
    PFFile *imageFile = [imageObject objectForKey:@"image4s"]; 
    NSString *UserProfile = [file url]; 

        if([[SDWebImageManager sharedManager] diskImageExistsForURL:[NSURL URLWithString:UserProfile]]) 
        { 
         NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:[NSURL URLWithString:UserProfile]]; 
         UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key]; 
         cell.imgView.image=image; 
        } 
        else 
        { 
         cell.imgView.image=[UIImage imageNamed:@"UserUpdationImageCell.png"]; 
         [cell.imgView sd_setImageWithURL:[NSURL URLWithString:UserProfile] placeholderImage:[UIImage imageNamed:@"UserUpdationImageCell.png"]]; 
        } 



    return cell; 
} 
+0

다른 이미지로 대체 된 셀의 컬렉션보기 이미지를 스크롤 할 때 한 가지 문제가 있습니다. – salmancs43

1

주요 thread.You을 차단할 수 있습니다 귀하의 다운로드 작업은 SDWebImage 또는 asynchrous 다운로드 작업 표시를 사용하려고 할 수 있습니다 추가하여 이미지

관련 문제