2013-03-15 39 views
0

我使用提供很好類asyncimageview:http://www.markj.net/iphone-asynchronous-table-image/的UITableView使用JSON文件,加載圖像到細胞消失

我從一個JSON文件中獲取圖像URL,且每個圖像加載到單元格。

問題:

當我向上滾動,然後滾動回落到相同的細胞中,圖像重新加載(消失,再次出現)。我無法弄清楚爲什麼圖像不斷重新加載?有沒有人有建議或解決方案,我可以如何讓它停止?提前致謝!

// Asks the data source to return a cell to insert in a particular table view location 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


    //Gets the current news article 
    NewsArticle *theCurrentArticle = [self.listofNewsArticles objectAtIndex:[indexPath row]]; 

    //Gets the title from the current article 
    NSString *theTitle = theCurrentArticle.title; 

    //Gets the image url 
    NSString *imageUrl = theCurrentArticle.imageURL; 

    //Gets the description of the current news article 
    NSString *theDescription = theCurrentArticle.description; 


    NewsCustomCell *cell = (NewsCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"NewsContent"]; 

    __block NewsCustomCell *aCell; 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

    if (cell == nil) { 
     aCell = (NewsCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"NewsContent"]; 
    } else { 
     AsyncImageView* oldImage = (AsyncImageView*) 
     [cell.contentView viewWithTag:999]; 
     [oldImage removeFromSuperview]; 
    } 



    AsyncImageView *imageView = [[AsyncImageView alloc] initWithFrame:CGRectMake(5, 5, 100, 100)]; 
    imageView.tag = 999; 

      dispatch_async(dispatch_get_main_queue(), ^{ 
    [imageView loadImageFromURL:[NSURL URLWithString:imageUrl]]; 
    [cell.contentView addSubview:imageView]; 

       cell.titleLabel.text = theTitle; 
       cell.descriptionLabel.text = theDescription; 
       cell.imageLabel.contentMode = UIViewContentModeScaleAspectFill; 

}); 

     }); 



    return cell; 



} 

繼承人的當前應用程序的樣子:

enter image description here

解決方案:

與此類我終於發現只是工作。 https://github.com/rs/SDWebImage 這處理緩存,異步下載。希望我能更快找到它。

+0

聽起來像所有的圖像都有標記999.如果您將該標記設置爲獨特的,該怎麼辦?就像每行索引路徑的序列化版本一樣? – abellina 2013-03-15 03:29:00

+0

嗯,我需要在那裏標記,所以如果它檢測到已經在視圖中的圖像,它會從超級視圖中刪除它。這是因爲你沒有圖像相互重疊。 – Jomoka 2013-03-15 03:50:05

回答

1

我希望你已經寫了AsyncImageView和ImageCache和ImageCacheObject類。 寫在代碼裏的cellForRowAtIndexPath:

static NSString *CellIdentifier = @"CellIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    Quest_Forum *Quest_ForumObj=[queArr objectAtIndex:i+indexPath.row]; 
    // NSLog(@"cell for row at... i val.. %d",i+indexPath.row); 

    for(UIView * view in cell.contentView.subviews){ 
     if([view isKindOfClass:[AsyncImageView class]]) 
     { 
      // NSLog(@"remove old images"); 
      [view removeFromSuperview]; 
      view = nil; 
     } 
    } 

    AsyncImageView *asyncImageView = nil; 

    UIImageView *cellImage = (UIImageView *)[cell viewWithTag:1]; // Inside tableview i have taken tableviewcell and given tag 1 to that imageview 

    asyncImageView = [[AsyncImageView alloc] initWithFrame:cellImage.frame] ; 
    [asyncImageView loadImageFromURL:Quest_ForumObj.Quest_Image]; 
    asyncImageView.backgroundColor=[UIColor clearColor]; 
    [cell.contentView addSubview:asyncImageView]; 


    UILabel *lblQuest = (UILabel *)[cell viewWithTag:2]; // Given tag 2 to the label inside tableViewCell 
    lblQuest.text=Quest_ForumObj.Question; 

在ImageCacheObject.h

@class ImageCacheObject; 

@interface ImageCache : NSObject 
{ 
    NSUInteger totalSize; // total number of bytes 
    NSUInteger maxSize; // maximum capacity 
    NSMutableDictionary *myDictionary; 
} 

@property (nonatomic, readonly) NSUInteger totalSize; 

-(id)initWithMaxSize:(NSUInteger) max; 
-(void)insertImage:(UIImage*)image withSize:(NSUInteger)sz forKey:(NSString*)key; 
-(UIImage*)imageForKey:(NSString*)key; 

在ImageCacheObject.m

#import "ImageCacheObject.h" 

@synthesize totalSize; 

-(id)initWithMaxSize:(NSUInteger) max 
{ 
    if (self = [super init]) 
    { 
     totalSize = 0; 
     maxSize = max; 
     myDictionary = [[NSMutableDictionary alloc] init]; 
    } 
    return self; 
} 

-(void)dealloc // Don't write this method if you are using ARC 
{ 
    [myDictionary release]; 
    [super dealloc]; 
} 

-(void)insertImage:(UIImage*)image withSize:(NSUInteger)sz forKey:(NSString*)key 
{ 
    // NSLog(@"count of insert image%d",sz); 
    ImageCacheObject *object = [[ImageCacheObject alloc] initWithSize:sz Image:image]; 
    while (totalSize + sz > maxSize) 
    { 
     NSDate *oldestTime; 
     NSString *oldestKey; 
     for (NSString *key in [myDictionary allKeys]) 
     { 
      ImageCacheObject *obj = [myDictionary objectForKey:key]; 
      if (oldestTime == nil || [obj.timeStamp compare:oldestTime] == NSOrderedAscending) 
      { 
       oldestTime = obj.timeStamp; 
       oldestKey = key; 
      } 
     } 
     if (oldestKey == nil) 
      break; // shoudn't happen 
     ImageCacheObject *obj = [myDictionary objectForKey:oldestKey]; 
     totalSize -= obj.size; 
     [myDictionary removeObjectForKey:oldestKey]; 
    } 
    [myDictionary setObject:object forKey:key]; 
    [object release]; 
} 

-(UIImage*)imageForKey:(NSString*)key 
{ 
    ImageCacheObject *object = [myDictionary objectForKey:key]; 
    if (object == nil) 
     return nil; 
    [object resetTimeStamp]; 
    return object.image; 
} 

在ImageCacheObject.h

@interface ImageCacheObject : NSObject 
{ 
    NSUInteger size; // size in bytes of image data 
    NSDate *timeStamp; // time of last access 
    UIImage *image;  // cached image 
} 

@property (nonatomic, readonly) NSUInteger size; 
@property (nonatomic, retain, readonly) NSDate *timeStamp; 
@property (nonatomic, retain, readonly) UIImage *image; 

-(id)initWithSize:(NSUInteger)sz Image:(UIImage*)anImage; 
-(void)resetTimeStamp; 

在ImageCacheObject.m中

@synthesize size; 
@synthesize timeStamp; 
@synthesize image; 

-(id)initWithSize:(NSUInteger)sz Image:(UIImage*)anImage 
{ 
    if (self = [super init]) 
    { 
     size = sz; 
     timeStamp = [[NSDate date] retain]; 
     image = [anImage retain]; 
    } 
    return self; 
} 

-(void)resetTimeStamp 
{ 
    [timeStamp release]; 
    timeStamp = [[NSDate date] retain]; 
} 

-(void) dealloc 
{ 
    [timeStamp release]; 
    [image release]; 
    [super dealloc]; 
} 
+0

不,我沒有圖像緩存或圖像緩存對象。 :/請你指點我一些資料來源?我也會環顧四周。謝謝! – Jomoka 2013-03-15 07:34:21

+0

我編輯了我的答案。請嘗試。 – 2013-03-15 07:42:56

+0

謝謝,我會嘗試一下! – Jomoka 2013-03-15 07:55:03