2012-01-14 23 views
0

我有TTTableView與一堆TTImageViews在其中。圖像視圖從遠程服務器上拖出圖像。我可以在瀏覽器中查看所有圖像。他們都加載非常快。TTImageView超時後

由於某些原因,嘗試加載圖像時,大約有10%的TTImageView超時。事情是,他們超時,真的很快。就像下一秒。當我加載TTTableView,我立刻得到這樣NSErrors(注:我已經改變了服務器和映像名稱):

Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x84232f0 {NSErrorFailingURLStringKey=http://www.myserver.com/myimage.jpg, NSErrorFailingURLKey=http://www.myserver.com/myimage.jpg, NSLocalizedDescription=The request timed out., NSUnderlyingError=0x849e640 "The request timed out."} 

因此,任何知道爲什麼TTImageViews零星立即超時?

+0

請問您可以在https://github.com/facebook/three20/issues上打開一個問題,或者在這裏引用它,如果這樣的問題已經存在? – tonklon 2012-02-07 08:15:42

回答

1

我不知道,如果這事做這一問題,但它聽起來可能有關。

我遇到的問題是,當發生2個(或更多)同時請求到同一圖像URL時,圖像不會加載任何請求。而錯誤總是相同的,你報道:

Code=-1001 "The request timed out." 

我發現,在TTRequestLoader.m的方法

- (void)connectToURL:(NSURL*)URL 

時有2個或多個請求相同的URL,代碼:

TTURLRequest* request = _requests.count == 1 ? [_requests objectAtIndex:0] : nil; 
NSURLRequest* URLRequest = [_queue createNSURLRequest:request URL:URL]; 

所以請求在「無」的值變量的結果,和createNSURLRequest未能設置超時。

其改爲:

TTURLRequest* request = [_requests lastObject]; 
NSURLRequest* URLRequest = [_queue createNSURLRequest:request URL:URL]; 

似乎工作確定

希望這有助於!

+0

不幸的是,我不能測試這個解決方案,因爲我不再能夠訪問我正在處理的項目,但這聽起來像是可行的。你是對的,一些URL指向同一個圖像。謝謝! – dnorcott 2012-05-09 20:00:54

1

是的!由於網絡問題,有時很奇怪。這裏是我的解決方案,我讓類符合TTImageViewDelegate和在 - (void)imageView:(TTImageView *)imageView didFailLoadWithError:(NSError *)error 方法,我用一個字典記錄它失敗了多少次加載特定的圖像。如果失敗次數不超過3次,則我調用[imageView reload]強制Three20重新請求圖像。通常它運作良好,我的意思是有時當它第二次請求時,圖像被成功加載。如果連續三次請求失敗,則放棄請求該映像,這意味着當時網絡非常糟糕。

#pragma mark TTImageView Delegate 

- (void)imageView:(TTImageView*)imageView didLoadImage:(UIImage*)image{ 
    [failLoadedImagesDict removeObjectForKey:imageView.urlPath]; 
} 

/** 
* Called when the image failed to load asynchronously. 
* If error is nil then the request was cancelled. 
*/ 
- (void)imageView:(TTImageView*)imageView didFailLoadWithError:(NSError*)error{ 
    NSString *urlPath = imageView.urlPath; 
    NSInteger count = [[failLoadedImagesDict valueForKey:urlPath] integerValue]; 
    if(count > 2) { 
    NSLog(@"Fail to load image for 3 times with error: %@", error); 
    [failLoadedImagesDict removeObjectForKey:urlPath]; 
    return; 
} 
count++; 
    [failLoadedImagesDict setObject:[NSNumber numberWithInteger:count] forKey:urlPath]; 
    [imageView reload]; 
} 
+0

是的,這是我必須執行的相同技巧。它大部分時間解決了這個問題,但我認爲@ alex的解決方案更全面。謝謝! – dnorcott 2012-05-09 20:01:56