SDWebImage

SDWebImage下载图片实现
- (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageDownloaderCompletedBlock)completedBlock {
__block SDWebImageDownloaderOperation *operation;
__weak SDWebImageDownloader *wself = self;

[self addProgressCallback:progressBlock andCompletedBlock:completedBlock forURL:url createCallback:^{
    NSTimeInterval timeoutInterval = wself.downloadTimeout;
    if (timeoutInterval == 0.0) {
        timeoutInterval = 15.0;
    }

    // In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests if told otherwise
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:(options & SDWebImageDownloaderUseNSURLCache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData) timeoutInterval:timeoutInterval];
    request.HTTPShouldHandleCookies = (options & SDWebImageDownloaderHandleCookies);
    request.HTTPShouldUsePipelining = YES;
    if (wself.headersFilter) {
        request.allHTTPHeaderFields = wself.headersFilter(url, [wself.HTTPHeaders copy]);
    }
    else {
        request.allHTTPHeaderFields = wself.HTTPHeaders;
    }
    operation = [[SDWebImageDownloaderOperation alloc] initWithRequest:request
                                                               options:options
                                                              progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                                                                  SDWebImageDownloader *sself = wself;
                                                                  if (!sself) return;
                                                                  NSArray *callbacksForURL = [sself callbacksForURL:url];
                                                                  for (NSDictionary *callbacks in callbacksForURL) {
                                                                      SDWebImageDownloaderProgressBlock callback = callbacks[kProgressCallbackKey];
                                                                      if (callback) callback(receivedSize, expectedSize);
                                                                  }
                                                              }
                                                             completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                                                                 SDWebImageDownloader *sself = wself;
                                                                 if (!sself) return;
                                                                 NSArray *callbacksForURL = [sself callbacksForURL:url];
                                                                 if (finished) {
                                                                     [sself removeCallbacksForURL:url];
                                                                 }
                                                                 for (NSDictionary *callbacks in callbacksForURL) {
                                                                     SDWebImageDownloaderCompletedBlock callback = callbacks[kCompletedCallbackKey];
                                                                     if (callback) callback(image, data, error, finished);
                                                                 }
                                                             }
                                                             cancelled:^{
                                                                 SDWebImageDownloader *sself = wself;
                                                                 if (!sself) return;
                                                                 [sself removeCallbacksForURL:url];
                                                             }];

    if (wself.username && wself.password) {
        operation.credential = [NSURLCredential credentialWithUser:wself.username password:wself.password persistence:NSURLCredentialPersistenceForSession];
    }

    if (options & SDWebImageDownloaderHighPriority) {
        operation.queuePriority = NSOperationQueuePriorityHigh;
    } else if (options & SDWebImageDownloaderLowPriority) {
        operation.queuePriority = NSOperationQueuePriorityLow;
    }

    [wself.downloadQueue addOperation:operation];
    if (wself.executionOrder == SDWebImageDownloaderLIFOExecutionOrder) {
        // Emulate LIFO execution order by systematically adding new operations as last operation's dependency
        [wself.lastAddedOperation addDependency:operation];
        wself.lastAddedOperation = operation;
    }
}];

return operation;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值