協(xié)議內(nèi)容
@protocol WXImageOperationProtocol <NSObject>
- (void)cancel;
@end
@protocol WXImgLoaderProtocol <WXModuleProtocol>
/**
* @abstract Creates a image download handler with a given URL
*
* @param imageUrl The URL of the image to download
*
* @param imageFrame The frame of the image you want to set
*
* @param options : The options to be used for this download
*
* @param completedBlock : A block called once the download is completed.
* image : the image which has been download to local.
* error : the error which has happened in download.
* finished : a Boolean value indicating whether download action has finished.
*/
- (id<WXImageOperationProtocol>)downloadImageWithURL:(NSString *)url imageFrame:(CGRect)imageFrame userInfo:(NSDictionary *)options completed:(void(^)(UIImage *image, NSError *error, BOOL finished))completedBlock;
@end
使用者1WXNavigationDefaultImpl
if (icon) {
WXBarButton *button = [WXBarButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 25, 25);
button.instanceId = param[@"instanceId"];
button.nodeRef = param[@"nodeRef"];
button.position = position;
[button addTarget:self action:@selector(onClickBarButton:) forControlEvents:UIControlEventTouchUpInside];
[[self imageLoader] downloadImageWithURL:icon imageFrame:CGRectMake(0, 0, 25, 25) userInfo:nil completed:^(UIImage *image, NSError *error, BOOL finished) {
dispatch_async(dispatch_get_main_queue(), ^{
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateHighlighted];
});
}];
return button;
}
下載button的icon
使用者2WXImageComponent
- (void)updateImage
{
__weak typeof(self) weakSelf = self;
dispatch_async(WXImageUpdateQueue, ^{
[self cancelImage];
void(^downloadFailed)(NSString *, NSError *) = ^void(NSString *url, NSError *error){
WXLogError(@"Error downloading image:%@, detail:%@", url, [error localizedDescription]);
};
NSString *imageSrc = weakSelf.imageSrc;
NSString *placeholderSrc = weakSelf.placeholdSrc;
if (weakSelf.placeholdSrc) {
WXLogDebug(@"Updating image, component:%@, placeholder:%@ ", self.ref, placeholderSrc);
weakSelf.placeholderOperation = [[weakSelf imageLoader] downloadImageWithURL:placeholderSrc imageFrame:weakSelf.calculatedFrame userInfo:nil completed:^(UIImage *image, NSError *error, BOOL finished) {
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(self) strongSelf = weakSelf;
UIImage *viewImage = ((UIImageView *)strongSelf.view).image;
if (error) {
downloadFailed(placeholderSrc,error);
if ([strongSelf isViewLoaded] && !viewImage) {
((UIImageView *)(strongSelf.view)).image = nil;
}
return;
}
if (![placeholderSrc isEqualToString:strongSelf.placeholdSrc]) {
return;
}
if ([strongSelf isViewLoaded] && !viewImage) {
((UIImageView *)strongSelf.view).image = image;
}
});
}];
}
if (weakSelf.imageSrc) {
WXLogDebug(@"Updating image:%@, component:%@", self.imageSrc, self.ref);
NSDictionary *userInfo = @{@"imageQuality":@(weakSelf.imageQuality), @"imageSharp":@(weakSelf.imageSharp)};
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.imageOperation = [[weakSelf imageLoader] downloadImageWithURL:imageSrc imageFrame:weakSelf.calculatedFrame userInfo:userInfo completed:^(UIImage *image, NSError *error, BOOL finished) {
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(self) strongSelf = weakSelf;
if (weakSelf.imageLoadEvent) {
[strongSelf fireEvent:@"load" params:@{ @"success": error? @"false" : @"true"}];
}
if (error) {
downloadFailed(imageSrc, error);
return ;
}
if (![imageSrc isEqualToString:strongSelf.imageSrc]) {
return ;
}
if ([strongSelf isViewLoaded]) {
((UIImageView *)strongSelf.view).image = image;
}
});
}];
});
}
if (!weakSelf.imageSrc && !weakSelf.placeholdSrc) {
dispatch_async(dispatch_get_main_queue(), ^{
self.layer.contents = nil;
});
}
});
}
實(shí)現(xiàn)
sdk沒有提供默認(rèn)的實(shí)現(xiàn),需要自定義實(shí)現(xiàn)方法喇闸。常見的做法是用SDWebImage這個(gè)第三方庫來做
- (id<WXImageOperationProtocol>)downloadImageWithURL:(NSString *)url imageFrame:(CGRect)imageFrame userInfo:(NSDictionary *)userInfo completed:(void(^)(UIImage *image, NSError *error, BOOL finished))completedBlock {
return (id<WXImageOperationProtocol>)[[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:url] options:SDWebImageRetryFailed progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (completedBlock) {
completedBlock(image, error, finished);
}
}];
}
疑問
imageFrame參數(shù)沒有處理蒸眠。一般可以在completed中對(duì)下載得到的圖片進(jìn)行壓縮什么的漾橙。當(dāng)然,只要資源圖片大小合適更好楞卡,不需要處理也行
協(xié)議的返回值沒有具體指定霜运,這里只是進(jìn)行了強(qiáng)制轉(zhuǎn)換。真的要
取消圖片下載蒋腮,具體該怎么操作需要進(jìn)一步細(xì)化淘捡。只是這種情況用到的比較少
@protocol SDWebImageOperation <NSObject>
- (void)cancel;
@end