2013-07-03 49 views
1

這是我在StackOver流程中的第一篇文章,我的英文不好。我是iOS的初學者,所以我慢一點。 其實我想從URL解析我的數據5(包括圖片)。我使用自定義tableView單元格。我的純文本顯示在自定義單元格(標籤)中,但圖像不是。這就是爲什麼我使用「dispatch_queue_t」來加載特定ImageView中的圖像。當我運行模擬器時,圖像顯示(Logo & BackGround)一個接一個地完美排列,但問題在我重置模擬器時發生。重置後,每個單元格中加載一個圖像(最後一個隊列)(共5個)。我在「viewDidLoad」中調用我的「parsingLoad」方法,以便它只在加載viewController時加載一次。任何人都可以告訴我我錯了什麼?使用「dispatch_queue_t」解析自定義表格單元格中的圖片效果不佳

這裏是我的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"FairListCustomCell"; 

FairListCustomCell *cell = (FairListCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    NSArray *topLabelObject = [[NSBundle mainBundle] loadNibNamed:@"FairListCustomCell" owner:self options:nil]; 

    for (id currentObject in topLabelObject) 
    { 
     if ([currentObject isKindOfClass:[UITableViewCell class]]) 
     { 
      cell = (FairListCustomCell*) currentObject; 
      break; 
     } 
    } 
} 

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

currentFair = [fairs objectAtIndex:indexPath.row]; 

cell.fairNameLabel.text =currentFair.FairName; 
cell.fairLocationLabel.text =currentFair.FairLocation; 
cell.bookmarkImageView.image=[UIImage imageNamed:@"Favorite.png"]; 
cell.bookmarkImageView.tag=indexPath.row; 

dispatch_queue_t imageQueueFairLogo = dispatch_queue_create("com.queue", NULL); 
dispatch_async(imageQueueFairLogo,^
{ 
    dispatch_retain(imageQueueFairLogo); 
    UIImage *imageFairLogo = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentFair FairLogo]]]]; 
    dispatch_async(dispatch_get_main_queue(),^
    { 
     cell.fairLogoImageView.image = imageFairLogo; 
     #if NEEDS_DISPATCH_RETAIN_RELEASE 
      dispatch_release(imageQueueFairLogo); 
     #endif 
    }); 
}); 

dispatch_queue_t imageQueueFairCellBackGround = dispatch_queue_create("com.queue", NULL); 
dispatch_async(imageQueueFairCellBackGround,^
{ 
    dispatch_retain(imageQueueFairCellBackGround); 
    UIImage *imageFairCellBackGround = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentFair FairCellBackGround]]]]; 
    dispatch_async(dispatch_get_main_queue(),^
    { 
     cell.fairCellBackGround.image = imageFairCellBackGround; 
     #if NEEDS_DISPATCH_RETAIN_RELEASE 
     dispatch_release(imageQueueFairCellBackGround); 
     #endif 
    }); 
}); 

return cell; 

}

我也嘗試用另一種方式拖。 月1日: - 使用 「global_queue」 兩個圖像的(這裏作爲一個樣本只放一個), 這樣的:

imageQueueFairCellBackGround = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
dispatch_async(imageQueueFairCellBackGround,^
{ 
    dispatch_retain(imageQueueFairCellBackGround); 
    UIImage *imageFairCellBackGround = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentFair FairCellBackGround]]]]; 
    dispatch_async(dispatch_get_main_queue(),^
    { 
     cell.fairCellBackGround.image = imageFairCellBackGround; 
     #if NEEDS_DISPATCH_RETAIN_RELEASE 
     dispatch_release(imageQueueFairCellBackGround); 
     #endif 
    }); 
}); 

return cell; 

第二: - 就像這樣:

dispatch_queue_t fairCellBackGroundcallerQueue = dispatch_get_current_queue(); 
dispatch_queue_t fairCellBackGrounddownloadQueue = dispatch_queue_create("Thumb downloader", NULL); 

dispatch_async(fairCellBackGrounddownloadQueue,^
{ 
    UIImage *imageFairCellBackGround = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentFair FairCellBackGround]]]]; 

    dispatch_async(fairCellBackGroundcallerQueue,^
    { 
     cell.fairCellBackGround.image = imageFairCellBackGround; 
    }); 
}); 
dispatch_release(fairCellBackGrounddownloadQueue); 

不過還是它不是沒有解決。 如果有人有任何解決方法,請與我分享。 非常感謝,先進的。

回答

4

更新的圖像,我發現這個特殊問題的解決方案之後。 這就是:

imageQueueFairLogo = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
    dispatch_async(imageQueueFairLogo,^
    { 
     UIImage *imageFairLogo = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentFair FairLogo]]]]; 
     dispatch_async(dispatch_get_main_queue(),^
     { 
      //[imageCache setObject:imageLogoCache forKey:currentFair.FairLogo]; 
      cell.fairLogoImageView.image = imageFairLogo; 
      [cell setNeedsLayout]; 
     }); 
    }); 

,當然還有你 「MyClass.h」 & 「MyClass.m」 文件中像這樣 「聲明」 & 「@synthesize」 對應的調度隊列:

@property(nonatomic) dispatch_queue_t imageQueueFairLogo; 
    @synthesize imageQueueFairLogo; 

對「imageQueueFairCellBackGround」做同樣的事情 謝謝。 :)

2

嘗試這樣

if (postAndCheckInDetails.postImageURL.length != 0) 
       { 
        if (!uploadImage) 
         uploadImage = (UIImageView*)[cell viewWithTag:4]; 

        dispatch_queue_t checkInQueueForPostImage = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 

        dispatch_async(checkInQueueForPostImage, ^{ 
         UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:postAndCheckInDetails.postImageURL]]]; 

         dispatch_sync(dispatch_get_main_queue(), ^{ 
          if (image!=nil) { 
           [uploadImage setImage:image]; 
           [uploadImage setFrame:CGRectMake(80, checkInDateAndTime.frame.origin.y+40, 160, 120)]; 
          } 

          [cell setNeedsLayout]; 
         }); 
        }); 
       } 
+0

我試着像你說: 我要補充細胞, 「uploadImage」 之前和註釋掉 「[uploadImage SETFRAME:CGRectMake(80,checkInDateAndTime.frame.origin.y + 40,160,120 )];」 因爲它無法識別「checkInDateAndTime」... 感謝您的評論。 :) 但是,它仍然無法正常工作,重置模擬器後。 – Tulon

0

註釋掉與dispatch_retain和dispatch_release線,並添加[細胞setNeedsDisplay]你

+0

我像這樣嘗試,如您所說: 'dispatch_queue_t imageQueueFairLogo = dispatch_queue_create(「com。排隊」,NULL); dispatch_async(imageQueueFairLogo,^ { // dispatch_retain(imageQueueFairLogo); 的UIImage * imageFairLogo = [UIImage的imageWithData:[NSData的dataWithContentsOfURL:[NSURL URLWithString:[currentFair FairLogo]]]]; dispatch_async( dispatch_get_main_queue(),^ { cell.fairLogoImageView.image = imageFairLogo; [細胞setNeedsDisplay]; }); });' 但是,不工作之後重置模擬器 感謝評論:)。 – Tulon

相關問題