2013-02-26 107 views
0

目前,我正在開發一個應用程序中,我有獲取Facebook相冊的照片作爲 圖庫查看異步使用表格視圖上使用下面的代碼表視圖單元格的按鈕&放圖片&顯示:加載圖像到自定義表視圖細胞異步

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSLog(@"particulars..:%d",[particular count]); 
    int count=self.particular.count%3; 

    if(count==0) 
    { 
     return self.particular.count/3; 
    } 
    else 
    { 
     return (self.particular.count/3) +1; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell1"; 
    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
     { 
      [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil]; 
      cell = customcell; 
     } 


    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 

     dispatch_async(dispatch_get_global_queue(0,0), ^{ 
      NSString *buttonurl=[self.particular objectAtIndex:((indexPath.row)*3)+0]; 
      NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: buttonurl]]; 
      if (data == nil) 
       return; 
      dispatch_async(dispatch_get_main_queue(), ^{ 


       UIImage *image = [UIImage imageWithData:data]; 
       cell.accessoryType = UITableViewCellAccessoryNone; 
       cell.selectionStyle = UITableViewCellSelectionStyleNone; 
       cell.backgroundColor=[UIColor clearColor]; 
       [cell.first setBackgroundImage:image forState:UIControlStateNormal] ; 
       [cell.first addTarget:self action:@selector(original:) forControlEvents:UIControlEventTouchUpInside]; 
       [cell.first setContentMode:UIViewContentModeCenter] ; 
       cell.first.layer.cornerRadius = 10.0; 
       cell.first.layer.borderColor = [[UIColor grayColor]CGColor]; 
       cell.first.layer.borderWidth = 1.0f; 
       cell.first.backgroundColor=[UIColor clearColor]; 
       cell.first.tag = ((indexPath.row)*3)+1 ; 
       [spinner stopAnimating]; 
      }); 
      [data release]; 
     }); 

     if(((indexPath.row)*3)+3<[particular count]) 
     { 
      UIActivityIndicatorView *spinner2 = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
      UIActivityIndicatorView *spinner3 = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
      //spinner2.backgroundColor=[UIColor blackColor]; 
      CGRect frame= cell.second.frame; 
      spinner2.frame=frame; 
      //spinner2.frame=CGRectMake(112, y, 106, 106); 
      spinner2.hidesWhenStopped = YES; 
      [cell.second addSubview:spinner2]; 
      [spinner2 startAnimating]; 

      CGRect frame2= cell.third.frame; 
      spinner3.frame=frame2; 
      spinner3.hidesWhenStopped = YES; 
      [cell.third addSubview:spinner3]; 
      [spinner3 startAnimating]; 

      dispatch_async(dispatch_get_global_queue(0,0),^
      { 
       NSString *buttonurl=[self.particular objectAtIndex:((indexPath.row)*3)+1]; 
       NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: buttonurl]]; 

       NSString *buttonurl1=[self.particular objectAtIndex:((indexPath.row)*3)+2]; 
       NSData * data1 = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: buttonurl1]]; 
       if (data == nil) 
        return; 
       dispatch_async(dispatch_get_main_queue(),^
       { 
        UIImage *image = [UIImage imageWithData:data]; 
        UIImage *image1 = [UIImage imageWithData:data1]; 

        [cell.second setBackgroundImage:image forState:UIControlStateNormal] ; 
        [cell.second addTarget:self action:@selector(original:) forControlEvents:UIControlEventTouchUpInside]; 
        [cell.second setContentMode:UIViewContentModeCenter] ; 
        cell.second.layer.cornerRadius = 10.0; 
        cell.second.layer.borderColor = [[UIColor grayColor]CGColor]; 
        cell.second.layer.borderWidth = 1.0f; 
        cell.second.backgroundColor=[UIColor clearColor]; 
        cell.second.tag = ((indexPath.row)*3)+2 ; 
        NSLog(@"cell.second.tag:%d",cell.second.tag); 

        [cell.third setBackgroundImage:image1 forState:UIControlStateNormal] ; 
        [cell.third addTarget:self action:@selector(original:) forControlEvents:UIControlEventTouchUpInside]; 
        [cell.third setContentMode:UIViewContentModeCenter] ; 
        cell.third.layer.cornerRadius = 10.0; 
        cell.third.layer.borderColor = [[UIColor grayColor]CGColor]; 
        cell.third.layer.borderWidth = 1.0f; 

        cell.third.backgroundColor=[UIColor clearColor]; 
        cell.third.tag = ((indexPath.row)*3)+3 ; 
        NSLog(@"cell.third.tag:%d",cell.third.tag); 
        [spinner2 stopAnimating]; 
        [spinner3 stopAnimating]; 

       }); 
       [data release]; 
      }); 


     } 
     else if(((indexPath.row)*3)+2<[particular count]) 
     { 
      UIActivityIndicatorView *spinner2 = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
      CGRect frame1 = CGRectMake(120, y, 106, 107); 
      spinner2.frame=frame1; 
      spinner2.hidesWhenStopped = YES; 
      [cell.second addSubview:spinner2]; 
      [spinner2 startAnimating]; 
      dispatch_async(dispatch_get_global_queue(0,0),^
      { 
       NSString *buttonurl=[self.particular objectAtIndex:((indexPath.row)*3)+1]; 
       NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: buttonurl]]; 
       if (data == nil) 
        return; 
       dispatch_async(dispatch_get_main_queue(),^
       { 
        UIImage *image = [UIImage imageWithData:data]; 
        [cell.second setBackgroundImage:image forState:UIControlStateNormal] ; 
        [cell.second addTarget:self action:@selector(original:) forControlEvents:UIControlEventTouchUpInside]; 
        [cell.second setContentMode:UIViewContentModeCenter] ; 
        cell.second.layer.cornerRadius = 10.0; 
        cell.second.layer.borderColor = [[UIColor grayColor]CGColor]; 
        cell.second.layer.borderWidth = 1.0f; 
        cell.second.backgroundColor=[UIColor clearColor]; 
        cell.second.tag = ((indexPath.row)*3)+2 ; 
        NSLog(@"cell.second.tag:%d",cell.second.tag); 
        [spinner2 stopAnimating]; 

       }); 
       [data release]; 
      });   
     } 
     else 
     { 
      cell.second.hidden = YES; 
      cell.third.hidden = YES; 
     } 
     y=y+106; 
     return cell; 
    } 

此代碼工作爲我好,但問題是,當圖像加載到按鈕的表視圖細胞含有那麼如果我們向下滾動,然後它慢慢地顯示圖像,但在那之後,當我們向上滾動查看先前負載圖像然後圖像消失&它再次加載從開始&拿請告訴我在代碼中犯的錯誤,或者我該如何解決這個問題?

+1

的圖片加載緩慢,它開始重新加載,因爲可重複使用的tableview細胞。它的好,如果你在數組中添加這些影像和數組加載到的tableview – Dany 2013-02-26 12:02:45

回答

3

我建議你使用這個AsyncImageView。我已經使用它,它創造奇蹟。要調用此API:

ASyncImage *img_EventImag = alloc with frame; 
NSURL *url = yourPhotoPath; 
[img_EventImage loadImageFromURL:photoPath]; 
[self.view addSubView:img_EventImage]; // In your case you'll add in your TableViewCell. 

與使用UIImageView相同。簡單,它爲你做大部分事情。 AsyncImageView在UIImageView中同時包含一個簡單的類別,用於在iOS上異步加載和顯示圖像,以便它們不鎖定UI,以及用於更高級功能的UIImageView子類。 AsyncImageView適用於URL,因此可以與本地或遠程文件一起使用。

加載/下載的圖像緩存在內存中,並在發生內存警告時自動清除。 AsyncImageView獨立於UIImage緩存運行,但默認情況下,位於應用程序捆綁包根目錄中的任何圖像都將存儲在UIImage緩存中,以避免重複緩存圖像。

該庫還可用於獨立於UIImageView加載和緩存圖像,因爲它提供對底層加載和緩存類的直接訪問。

3

有哪些做的工作的一些自定義庫很好的爲你

  1. AsynchImageView
  2. SDWebImage
  3. AFNetworking圖像擴展(如果你使用AF)
+1

Lithu是正確的。你可以使用任何一個可以輕鬆完成工作的任務。 – 2013-02-26 13:19:36

1

據工作它應該做的方式。當您滾動表格視圖時,就像消失的單元格已被釋放,並且向後滾動單元格時出現。 所以,你應該用一個解決方案來緩存圖像,使他們不需要再次加載,如EGOImageView(here),AsyncImageView,SDWebImage等做

0

這是一個使用dispatch_async的解決方案。如果你正在從服務的URL不是用這種方法NSObject的地方服務被稱爲

-(void)loadProfilePicWithURL:(NSString *)strUrl tagV:(NSInteger)tagV{ 
isProfilePicLoaded = NO; 
//set media tag as indexPath.row 

//call this method from cellForRowAtIndexpPath 
//Pass the image URL --> strProfilePicURL 
//Load the image in bg and set to imgProfilePic 
//After loaded, send a notification with tag to table view to update the cell 


NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:strUrl]]; 
imgProfilePic=[UIImage imageWithData:data]; 

isProfilePicLoaded = YES; 
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTableViewCell" object:[NSNumber numberWithInteger:tagV]]; 
} 

呼叫從的cellForRowAtIndexPath

[self performSelectorInBackground:@selector(loadPic:) withObject:[NSNumber numberWithInteger:indexPath.row]]; 

這種方法實現

-(void)loadPic: (id)indexPath{ 
NSInteger conV=[indexPath integerValue]; 

NSObject *object=[Array objectAtIndex:conV]; 
[object loadProfilePicWithURL:object.url tagV:conV]; 

}

添加觀察員通知NSObject

-(void)reloadTableViewCell:(NSNotification *)note{ 
NSInteger mediaTag = [[note object] integerValue]; 

[self reloadCellImage:mediaTag]; 

}

重新加載該表中的特定細胞。

-(void)reloadCellImage:(NSInteger)row{ 
NSObject *object = [self.receivedArray objectAtIndex:row]; 
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:row inSection:0]; 
customCell *cell = (customCell *)[self.tableView cellForRowAtIndexPath:rowToReload]; 
[cell.imgVw setImage:object.image];}