diff --git a/Examples/SDWebImage Watch Demo Extension/InterfaceController.m b/Examples/SDWebImage Watch Demo Extension/InterfaceController.m index 31aeb716..521de388 100644 --- a/Examples/SDWebImage Watch Demo Extension/InterfaceController.m +++ b/Examples/SDWebImage Watch Demo Extension/InterfaceController.m @@ -29,9 +29,11 @@ // This method is called when watch view controller is about to be visible to user [super willActivate]; - NSString *urlString = @"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png"; - [[SDWebImageManager sharedManager] loadImageWithURL:[NSURL URLWithString:urlString] options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) { - self.imageInterface.image = image; + NSString *urlString = @"http://apng.onevcat.com/assets/elephant.png"; + WKInterfaceImage *imageInterface = self.imageInterface; + [imageInterface sd_setImageWithURL:[NSURL URLWithString:urlString] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { + // `WKInterfaceImage` unlike `UIImageView`. Even the image is animated image, you should explicitly call `startAnimating` to play animation. + [imageInterface startAnimating]; }]; } diff --git a/SDWebImage/SDWebImageCompat.h b/SDWebImage/SDWebImageCompat.h index 7a68f34d..6fd79b6b 100644 --- a/SDWebImage/SDWebImageCompat.h +++ b/SDWebImage/SDWebImageCompat.h @@ -72,6 +72,12 @@ #endif #if SD_WATCH #import + #ifndef UIView + #define UIView WKInterfaceObject + #endif + #ifndef UIImageView + #define UIImageView WKInterfaceImage + #endif #endif #endif diff --git a/SDWebImage/UIImageView+WebCache.h b/SDWebImage/UIImageView+WebCache.h index 204c2278..1347f18d 100644 --- a/SDWebImage/UIImageView+WebCache.h +++ b/SDWebImage/UIImageView+WebCache.h @@ -7,9 +7,6 @@ */ #import "SDWebImageCompat.h" - -#if SD_UIKIT || SD_MAC - #import "SDWebImageManager.h" /** @@ -197,5 +194,3 @@ #endif @end - -#endif diff --git a/SDWebImage/UIImageView+WebCache.m b/SDWebImage/UIImageView+WebCache.m index 31bcabb9..63c19239 100644 --- a/SDWebImage/UIImageView+WebCache.m +++ b/SDWebImage/UIImageView+WebCache.m @@ -7,9 +7,6 @@ */ #import "UIImageView+WebCache.h" - -#if SD_UIKIT || SD_MAC - #import "objc/runtime.h" #import "UIView+WebCacheOperation.h" #import "UIView+WebCache.h" @@ -138,5 +135,3 @@ static char animationLoadOperationKey; #endif @end - -#endif diff --git a/SDWebImage/UIView+WebCache.h b/SDWebImage/UIView+WebCache.h index cf212146..d674d9a1 100644 --- a/SDWebImage/UIView+WebCache.h +++ b/SDWebImage/UIView+WebCache.h @@ -7,9 +7,6 @@ */ #import "SDWebImageCompat.h" - -#if SD_UIKIT || SD_MAC - #import "SDWebImageDefine.h" #import "SDWebImageManager.h" #import "SDWebImageTransition.h" @@ -77,6 +74,8 @@ typedef void(^SDSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable ima */ - (void)sd_cancelCurrentImageLoad; +#if SD_UIKIT || SD_MAC + #pragma mark - Image Transition /** @@ -94,6 +93,6 @@ typedef void(^SDSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable ima */ @property (nonatomic, strong, nullable) id sd_imageIndicator; -@end - #endif + +@end diff --git a/SDWebImage/UIView+WebCache.m b/SDWebImage/UIView+WebCache.m index 2f5bc03f..dfe1ae7b 100644 --- a/SDWebImage/UIView+WebCache.m +++ b/SDWebImage/UIView+WebCache.m @@ -7,9 +7,6 @@ */ #import "UIView+WebCache.h" - -#if SD_UIKIT || SD_MAC - #import "objc/runtime.h" #import "UIView+WebCacheOperation.h" #import "SDWebImageError.h" @@ -79,8 +76,11 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; self.sd_imageProgress.totalUnitCount = 0; self.sd_imageProgress.completedUnitCount = 0; +#if SD_UIKIT || SD_MAC // check and start image indicator [self sd_startImageIndicator]; + id imageIndicator = self.sd_imageIndicator; +#endif SDWebImageManager *manager; if ([context valueForKey:SDWebImageContextCustomManager]) { @@ -89,21 +89,23 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; manager = [SDWebImageManager sharedManager]; } - id imageIndicator = self.sd_imageIndicator; - __weak __typeof(self)wself = self; SDWebImageDownloaderProgressBlock combinedProgressBlock = ^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) { - wself.sd_imageProgress.totalUnitCount = expectedSize; - wself.sd_imageProgress.completedUnitCount = receivedSize; + __strong __typeof (wself) sself = wself; + NSProgress *imageProgress = sself.sd_imageProgress; + imageProgress.totalUnitCount = expectedSize; + imageProgress.completedUnitCount = receivedSize; if (progressBlock) { progressBlock(receivedSize, expectedSize, targetURL); } +#if SD_UIKIT || SD_MAC if ([imageIndicator respondsToSelector:@selector(updateIndicatorProgress:)]) { - double progress = wself.sd_imageProgress.fractionCompleted; + double progress = imageProgress.fractionCompleted; dispatch_async(dispatch_get_main_queue(), ^{ [imageIndicator updateIndicatorProgress:progress]; }); } +#endif }; id operation = [manager loadImageWithURL:url options:options context:context progress:combinedProgressBlock completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { __strong __typeof (wself) sself = wself; @@ -114,10 +116,12 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; sself.sd_imageProgress.completedUnitCount = SDWebImageProgressUnitCountUnknown; } +#if SD_UIKIT || SD_MAC // check and stop image indicator if (finished) { [self sd_stopImageIndicator]; } +#endif BOOL shouldCallCompletedBlock = finished || (options & SDWebImageAvoidAutoSetImage); BOOL shouldNotSetImage = ((image && (options & SDWebImageAvoidAutoSetImage)) || @@ -152,16 +156,22 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; targetData = nil; } +#if SD_UIKIT || SD_MAC // check whether we should use the image transition SDWebImageTransition *transition = nil; if (finished && (options & SDWebImageForceTransition || cacheType == SDImageCacheTypeNone)) { transition = sself.sd_imageTransition; } +#endif if (group) { dispatch_group_enter(group); dispatch_main_async_safe(^{ +#if SD_UIKIT || SD_MAC [sself sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock transition:transition cacheType:cacheType imageURL:imageURL]; +#else + [sself sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock]; +#endif }); // ensure completion block is called after custom setImage process finish dispatch_group_notify(group, dispatch_get_main_queue(), ^{ @@ -169,14 +179,20 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; }); } else { dispatch_main_async_safe(^{ +#if SD_UIKIT || SD_MAC [sself sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock transition:transition cacheType:cacheType imageURL:imageURL]; +#else + [sself sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock]; +#endif callCompletedBlockClojure(); }); } }]; [self sd_setImageLoadOperation:operation forKey:validOperationKey]; } else { +#if SD_UIKIT || SD_MAC [self sd_stopImageIndicator]; +#endif dispatch_main_async_safe(^{ if (completedBlock) { NSError *error = [NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorInvalidURL userInfo:@{NSLocalizedDescriptionKey : @"Image url is nil"}]; @@ -191,23 +207,31 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; } - (void)sd_setImage:(UIImage *)image imageData:(NSData *)imageData basedOnClassOrViaCustomSetImageBlock:(SDSetImageBlock)setImageBlock { +#if SD_UIKIT || SD_MAC [self sd_setImage:image imageData:imageData basedOnClassOrViaCustomSetImageBlock:setImageBlock transition:nil cacheType:0 imageURL:nil]; +#else + // watchOS does not support view transition. Simplify the logic + if (setImageBlock) { + setImageBlock(image, imageData); + } else if ([self isKindOfClass:[UIImageView class]]) { + UIImageView *imageView = (UIImageView *)self; + [imageView setImage:image]; + } +#endif } +#if SD_UIKIT || SD_MAC - (void)sd_setImage:(UIImage *)image imageData:(NSData *)imageData basedOnClassOrViaCustomSetImageBlock:(SDSetImageBlock)setImageBlock transition:(SDWebImageTransition *)transition cacheType:(SDImageCacheType)cacheType imageURL:(NSURL *)imageURL { UIView *view = self; SDSetImageBlock finalSetImageBlock; if (setImageBlock) { finalSetImageBlock = setImageBlock; - } -#if SD_UIKIT || SD_MAC - else if ([view isKindOfClass:[UIImageView class]]) { + } else if ([view isKindOfClass:[UIImageView class]]) { UIImageView *imageView = (UIImageView *)view; finalSetImageBlock = ^(UIImage *setImage, NSData *setImageData) { imageView.image = setImage; }; } -#endif #if SD_UIKIT else if ([view isKindOfClass:[UIButton class]]) { UIButton *button = (UIButton *)view; @@ -273,15 +297,20 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; } } } +#endif - (void)sd_setNeedsLayout { #if SD_UIKIT [self setNeedsLayout]; #elif SD_MAC [self setNeedsLayout:YES]; +#elif SD_WATCH + // Do nothing because WatchKit automatically layout the view after property change #endif } +#if SD_UIKIT || SD_MAC + #pragma mark - Image Transition - (SDWebImageTransition *)sd_imageTransition { return objc_getAssociatedObject(self, @selector(sd_imageTransition)); @@ -340,6 +369,6 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; }); } -@end - #endif + +@end diff --git a/SDWebImage/UIView+WebCacheOperation.h b/SDWebImage/UIView+WebCacheOperation.h index 18361829..65872521 100644 --- a/SDWebImage/UIView+WebCacheOperation.h +++ b/SDWebImage/UIView+WebCacheOperation.h @@ -7,10 +7,7 @@ */ #import "SDWebImageCompat.h" - -#if SD_UIKIT || SD_MAC - -#import "SDWebImageManager.h" +#import "SDWebImageOperation.h" // These methods are used to support canceling for UIView image loading, it's designed to be used internal but not external. // All the stored operations are weak, so it will be dalloced after image loading finished. If you need to store operations, use your own class to keep a strong reference for them. @@ -47,5 +44,3 @@ - (void)sd_removeImageLoadOperationWithKey:(nullable NSString *)key; @end - -#endif diff --git a/SDWebImage/UIView+WebCacheOperation.m b/SDWebImage/UIView+WebCacheOperation.m index b4f25edc..d83c8e0c 100644 --- a/SDWebImage/UIView+WebCacheOperation.m +++ b/SDWebImage/UIView+WebCacheOperation.m @@ -7,9 +7,6 @@ */ #import "UIView+WebCacheOperation.h" - -#if SD_UIKIT || SD_MAC - #import "objc/runtime.h" static char loadOperationKey; @@ -80,5 +77,3 @@ typedef NSMapTable> SDOperationsDictionary; } @end - -#endif