2013-12-16 30 views
2

的CollectionView來電圖像如何檢查網絡服務,數據庫,如果數據庫中有將顯示在數據庫中的圖像而不是其將顯示圖像從Web服務與數據庫

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 

BookCell *bookcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ReuseCell" forIndexPath:indexPath]; 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     NSURL *imageURL = [NSURL URLWithString:[[bookList objectAtIndex:indexPath.item]coverURL]]; 
     NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; 
     UIImage *image = [UIImage imageWithData:imageData]; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *path = [paths objectAtIndex:0]; 
     NSString *bookPath2 = [NSString stringWithFormat:@"%@/assets/book/%@/cover.png",path,bookPath]; 
     NSLog(@"bookpath2 = %@",bookPath2); 


     dispatch_async(dispatch_get_main_queue(), ^{ 

      if ([db isDownloaded:bookName bookVersion:[bookVersion floatValue]]) { 

      [bookcell.bookCover setImage:image]; 
      [bookcell.bookCover setAlpha:1.0f]; 
// or this [bookcell.bookCover setImage:[UIImage imageWithContentsOfFile:bookPath2]]; 

      } 
      else{ 
       [bookcell.bookCover setImage:image]; 
       [bookcell.bookCover setAlpha:0.5f]; 
       [bookcell.bookDetailLabel setHidden:NO]; 
       [bookcell.bookDetailLabel setAlpha:1.0f];  
      } 

     }); 
    }); 

    return bookcell; 

} 

建議我請。 請幫我。

回答

1

我試試這是我的工作,謝謝!對於任何想法

if (bookPath2 = [NSString stringWithFormat:@"%@/assets/book/%@/cover.png",path],[[bookList objectAtIndex:indexPath.item] bookPath] && bookPath2 != [NSString stringWithFormat:@"%@/assets/book/(null)/cover.png",path]) { 
      NSLog(@"++++++++++++++++++++++"); 
      bookcell.bookCover.image = [UIImage imageWithContentsOfFile:bookPath2]; 

      if (checkversion < checkNewversion) { 
       bookcell.bookDetailLabel.text = @"Update"; 
       bookcell.bookDetailLabel.hidden = NO; 
      }    else 
      { 
       bookcell.bookDetailLabel.hidden = YES; 

      } 
     } 

     else { 
      bookcell.bookCover.image = nil; 
     NSLog(@"-------------------------"); 
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

      NSURL *imageURL = [NSURL URLWithString:[[bookList objectAtIndex:indexPath.item]coverURL]]; 
      NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; 
      UIImage *image = [UIImage imageWithData:imageData]; 

       dispatch_async(dispatch_get_main_queue(), ^{ 

        bookcell.bookDetailLabel.text = @"New"; 
        bookcell.bookCover.image = [UIImage imageWithData:imageData]; 
        bookcell.bookCover.alpha = 0.5f; 
        bookcell.bookDetailLabel.hidden = NO; 


      }); 
     }); 

     } 
     return bookcell;