2013-04-26 200 views
0

我正在使用SDWebImage庫在iOS應用中緩存圖像。現在,當從服務器加載圖像時,我在imageview上顯示了一個活動指示器。我正在使用以下代碼iOS設置SDWebImage中的URL請求超時

__block UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
    activityIndicator.center = self.serviceImageView.center; 
    activityIndicator.hidesWhenStopped = YES; 
    [activityIndicator startAnimating]; 
    [self.serviceImageView addSubview:activityIndicator]; 
    [self.serviceImageView setImageWithURL:[NSURL URLWithString:[self.service valueForKey:@"image"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) { 
    [activityIndicator removeFromSuperview]; 
    }]; 

它工作正常。但對於一些圖像,它沒有加載圖像,所以它永遠不會完成塊&活動指示器旋轉無限。 那麼有沒有什麼辦法可以設置一個超時時間,以便在一段時間後它給了我一個錯誤或類似的東西,這樣我就可以停止活動指示器。

這是有點銀色。請幫忙。

回答

0

您應該檢查URL是否有效,因爲當您通過nilsetImageWithURL時,不會調用完成塊。

NSURL* url = [NSURL URLWithString:[self.service valueForKey:@"image"]]; 

if (url) 
    [self.serviceImageView setImageWithURL:url placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) { 
    [activityIndicator removeFromSuperview]; 
    }]; 
I 
+0

該網址是好的,但這並沒有返回任何圖像而我只是看到它的作品第一次罰款。之後,SDWebImage將URL列入黑名單,因此它不會調用完成塊。我不得不使用選項SDWebImageRetryFailed – 2013-04-26 12:05:33