我認爲我在問一個愚蠢的問題,但只是好奇。 有什麼方法可以知道是否下載了所有圖像。 我的動機是,我想在所有圖像下載後調用一個函數。如何知道AfNetworking是否已經下載了所有圖像
我在使用UIImageview setImageWithURL
for循環
感謝提前:)
我認爲我在問一個愚蠢的問題,但只是好奇。 有什麼方法可以知道是否下載了所有圖像。 我的動機是,我想在所有圖像下載後調用一個函數。如何知道AfNetworking是否已經下載了所有圖像
我在使用UIImageview setImageWithURL
for循環
感謝提前:)
你要使用的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;
由於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.
}];
}
可以使用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]
這只是一個僞代碼。我沒有測試過,但它應該指向正確的方向。
看我的答案在這裏:http://stackoverflow.com/a/21832526/3319153 希望這將有助於。 –