2014-02-17 95 views
2

我認爲我在問一個愚蠢的問題,但只是好奇。 有什麼方法可以知道是否下載了所有圖像。 我的動機是,我想在所有圖像下載後調用一個函數。如何知道AfNetworking是否已經下載了所有圖像

我在使用UIImageview setImageWithURL for循環

感謝提前:)

+0

看我的答案在這裏:http://stackoverflow.com/a/21832526/3319153 希望這將有助於。 –

回答

1

你要使用的UIImageView + AFNetworking的setImageWithURLRequest方法。當圖像加載完成時,成功塊會被執行。

@param urlRequest The URL request used for the image request. 
@param placeholderImage The image to be set initially, until the image request finishes. If `nil`, the image view will not change its image until the image request finishes. 
@param success A block to be executed when the image request operation finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the image created from the response data of request. If the image was returned from cache, the request and response parameters will be `nil`. 
@param failure A block object to be executed when the image request operation finishes unsuccessfully, or that finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error object describing the network or parsing error that occurred. 
*/ 
- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest 
       placeholderImage:(UIImage *)placeholderImage 
         success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success 
         failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; 
+0

但我如何追蹤上次下載的圖片? – DAMM108

+0

問題是您正在加載n張圖片,並且您想知道所有n張圖片何時完成下載? – peterxv

+0

如果您只是試圖確定n個圖像何時完成下載。你可以創建一個實例變量downloadCount = 0,numImages = n。每當成功塊執行增量downloadCount並且downloadCount == numImages,那麼你就完成了。這可能是最簡單的解決方案,但它會起作用。 – peterxv

1

由於setImageWithURL是異步的,這使得它有點難以跟蹤的所有圖像是否已被下載。可能有效的一種解決方案是使用setImageWithURLRequest:placeholderImage:success:failure:方法,該方法允許您在圖像URL請求成功或失敗時執行代碼。

由於您正在運行for循環,因此您可能會有固定數量的圖片通過。在這種情況下,您可以設置一個屬性,用於跟蹤已在success/failure塊中下載的圖像。當這個值等於你想要的數字時,你可以運行某種邏輯(即發佈通知,委託)來觸發所有下載已經完成。 (或者,如果有任何失敗,則可以添加一些邏輯重試/發佈消息說發生了錯誤等)

例如(假設numberOfImagesToDownload是一些恆定值集合):

- (void)processImageForURL:(NSURL *)url { 
    // Assume `placeholderImage` is a reference to an image. 
    [imageView setImageWithURLRequest:[NSURLRequest requestWithURL:url] 
        placeholderImage:placeholderImage 
           success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
     // Logic here to set the imageView's image (for the example's sake, assume 
     // we have access to the respective UIImageView.) 
     imageView.image = image; 

     // Hold onto a property to keep track of how many images you've downloaded, 
     // under the assumption that there's a set number of images you need to download. 
     // Since you're running this under a for loop, you could probably check if the 
     // for's max condition is equal to the number of downloaded images. 
     self.numberOfImagesDownloaded++; 
     if(self.numberOfImagesDownloaded == numberOfImagesToDownload) { 
      // All images have been downloaded. 
     } 
    } 
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
     // You can also keep track of which images failed, if that's important. 
    }]; 
} 
1

可以使用NSOperationQueue排隊您的要求和使用KVO來觀察你的隊列的業務屬性,那麼就可以判斷你的隊列通過檢查[queue.operations count] == 0完成。

添加的操作特性的觀察員:

[self.queue addObserver:self forKeyPath:@"operations" options:0 context:NULL]; 

處理該事件時,操作計數命中0:

- (void) observeValueForKeyPath:(NSString *)keyPath 
         ofObject:(id)object 
         change:(NSDictionary *)change 
         context:(void *)context 
{ 
    if (object == self.queue && [keyPath isEqualToString:@"operations"]) 
    { 
     if ([self.queue.operations count] == 0) 
     { 
      // Your downloads have completed 
     } 
    } 
    else 
    { 
     [super observeValueForKeyPath:keyPath 
          ofObject:object 
           change:change 
           context:context]; 
    } 
} 

而且你把你的要求是這樣的:

AFImageRequestOperation *imgRequest = [AFImageRequestOperation imageRequestOperationWithRequest:urlRequest success:^(UIImage *image) { } 

[self.queue addOperation: imgRequest] 

這只是一個僞代碼。我沒有測試過,但它應該指向正確的方向。

+0

AFnetworking中是否有全局隊列?[只是好奇] – DAMM108

+0

@ DAMM108是的,如果你使用AFNetworking 1.x(它在AFHTTPClient上)或AFNetworking 2.x AFHTTPRequestOperationManager。但是,如果您在AFHTTPSessionManager中使用AFNetworking 2.x,則不會。 (好吧,還沒有,它正在開發中。) –

+0

@Aaron Brager,我正在使用AFNetworking 1.x – DAMM108

相關問題