美文网首页
SDWebImage前世今生之V2.6版本

SDWebImage前世今生之V2.6版本

作者: _相信自己_ | 来源:发表于2019-03-04 11:42 被阅读0次

2.6版本2012年5月4日发布

1. SDWebImageManager

  • block调用以及释放(MRC)
  • block回调支持
#if NS_BLOCKS_AVAILABLE
- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate options:(SDWebImageOptions)options success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure{
    //-----------------------2.6版本更新-优化-----------------------
    //非常常见的错误是使用NSString对象而不是NSURL发送URL。出于某种奇怪的原因,XCode不会这样做
    //为这种类型不匹配抛出任何警告。在这里,我们通过允许url作为NSString传递来防止这个错误。
    if ([url isKindOfClass:NSString.class]){
        url = [NSURL URLWithString:(NSString *)url];
    }
    if (!url || !delegate || (!(options & SDWebImageRetryFailed) && [failedURLs containsObject:url])){
        return;
    }
    //检查磁盘上的缓存异步,这样我们就不会阻塞主线程
    [cacheDelegates addObject:delegate];
    [cacheURLs addObject:url];
    SuccessBlock successCopy = [success copy];
    FailureBlock failureCopy = [failure copy];
    NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:delegate, @"delegate", url, @"url", [NSNumber numberWithInt:options], @"options", successCopy, @"success", failureCopy, @"failure", nil];
    SDWIRelease(successCopy);
    SDWIRelease(failureCopy);
    [[SDImageCache sharedImageCache] queryDiskCacheForKey:[url absoluteString] delegate:self userInfo:info];
}
#endif

- (void)imageDownloader:(SDWebImageDownloader *)downloader didFinishWithImage:(UIImage *)image{
    SDWIRetain(downloader);
    SDWebImageOptions options = [[downloader.userInfo objectForKey:@"options"] intValue];

            if (image){
                if ([delegate respondsToSelector:@selector(webImageManager:didFinishWithImage:)]){
                    [delegate performSelector:@selector(webImageManager:didFinishWithImage:) withObject:self withObject:image];
                }
                if ([delegate respondsToSelector:@selector(webImageManager:didFinishWithImage:forURL:)]){
                    objc_msgSend(delegate, @selector(webImageManager:didFinishWithImage:forURL:), self, image, downloader.url);
                }
                //--------------2.6版本更新-block支持优化----------------
#if NS_BLOCKS_AVAILABLE
                if ([downloader.userInfo objectForKey:@"success"]){
                    SuccessBlock success = [downloader.userInfo objectForKey:@"success"];
                    success(image);
                }
#endif
                //-------------------------end------------------------
            } else {
                if ([delegate respondsToSelector:@selector(webImageManager:didFailWithError:)]){
                    [delegate performSelector:@selector(webImageManager:didFailWithError:) withObject:self withObject:nil];
                }
                if ([delegate respondsToSelector:@selector(webImageManager:didFailWithError:forURL:)]){
                    objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:), self, nil, downloader.url);
                }
                //--------------2.6版本更新-block支持优化----------------
#if NS_BLOCKS_AVAILABLE
                if ([downloader.userInfo objectForKey:@"failure"]){
                    FailureBlock failure = [downloader.userInfo objectForKey:@"failure"];
                    failure(nil);
                }
#endif
                //-------------------------end------------------------
            }
           
    // 释放下载器
    [downloaderForURL removeObjectForKey:downloader.url];
    SDWIRelease(downloader);
}

- (void)imageDownloader:(SDWebImageDownloader *)downloader didFailWithError:(NSError *)error{
    SDWIRetain(downloader);
    //用这个下载器通知所有下载delegate
    for (NSInteger idx = (NSInteger)[downloaders count] - 1; idx >= 0; idx--){
        NSUInteger uidx = (NSUInteger)idx;
        SDWebImageDownloader *aDownloader = [downloaders objectAtIndex:uidx];
        if (aDownloader == downloader){
            id<SDWebImageManagerDelegate> delegate = [downloadDelegates objectAtIndex:uidx];
            SDWIRetain(delegate);
            SDWIAutorelease(delegate);
            if ([delegate respondsToSelector:@selector(webImageManager:didFailWithError:)]){
                [delegate performSelector:@selector(webImageManager:didFailWithError:) withObject:self withObject:error];
            }
            if ([delegate respondsToSelector:@selector(webImageManager:didFailWithError:forURL:)]){
                objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:), self, error, downloader.url);
            }
            //--------------2.6版本更新-block支持优化----------------
#if NS_BLOCKS_AVAILABLE
            if ([downloader.userInfo objectForKey:@"failure"]){
                FailureBlock failure = [downloader.userInfo objectForKey:@"failure"];
                failure(error);
            }
#endif
            //------------------------end-------------------------
            [downloaders removeObjectAtIndex:uidx];
            [downloadDelegates removeObjectAtIndex:uidx];
        }
    }
    [downloaderForURL removeObjectForKey:downloader.url];
    SDWIRelease(downloader);
}

2. SDImageCache

  • 新增磁盘删除功能:删除本地文件
//-----------------------2.6版本更新-方法重载---------------------
- (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk;
//-------------------------------end---------------------------
//-----------------------2.6版本更新-方法重载---------------------
- (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk{
    if (key == nil){
        return;
    }
    [memCache removeObjectForKey:key];
    if (fromDisk){
        [[NSFileManager defaultManager] removeItemAtPath:[self cachePathForKey:key] error:nil];
    }
}
//-----------------------------end-----------------------------

相关文章

网友评论

      本文标题:SDWebImage前世今生之V2.6版本

      本文链接:https://www.haomeiwen.com/subject/efkjuqtx.html