Merge pull request #2331 from dreampiggy/feature_watchkit_setImage
Feature watchOS WKInterfaceImage with sd_setImageWithURL
This commit is contained in:
commit
ee6bca1518
|
@ -29,9 +29,11 @@
|
||||||
// This method is called when watch view controller is about to be visible to user
|
// This method is called when watch view controller is about to be visible to user
|
||||||
[super willActivate];
|
[super willActivate];
|
||||||
|
|
||||||
NSString *urlString = @"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png";
|
NSString *urlString = @"http://apng.onevcat.com/assets/elephant.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) {
|
WKInterfaceImage *imageInterface = self.imageInterface;
|
||||||
self.imageInterface.image = image;
|
[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];
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -72,6 +72,12 @@
|
||||||
#endif
|
#endif
|
||||||
#if SD_WATCH
|
#if SD_WATCH
|
||||||
#import <WatchKit/WatchKit.h>
|
#import <WatchKit/WatchKit.h>
|
||||||
|
#ifndef UIView
|
||||||
|
#define UIView WKInterfaceObject
|
||||||
|
#endif
|
||||||
|
#ifndef UIImageView
|
||||||
|
#define UIImageView WKInterfaceImage
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -7,9 +7,6 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#import "SDWebImageCompat.h"
|
#import "SDWebImageCompat.h"
|
||||||
|
|
||||||
#if SD_UIKIT || SD_MAC
|
|
||||||
|
|
||||||
#import "SDWebImageManager.h"
|
#import "SDWebImageManager.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -197,5 +194,3 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
|
@ -7,9 +7,6 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#import "UIImageView+WebCache.h"
|
#import "UIImageView+WebCache.h"
|
||||||
|
|
||||||
#if SD_UIKIT || SD_MAC
|
|
||||||
|
|
||||||
#import "objc/runtime.h"
|
#import "objc/runtime.h"
|
||||||
#import "UIView+WebCacheOperation.h"
|
#import "UIView+WebCacheOperation.h"
|
||||||
#import "UIView+WebCache.h"
|
#import "UIView+WebCache.h"
|
||||||
|
@ -138,5 +135,3 @@ static char animationLoadOperationKey;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
|
@ -7,9 +7,6 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#import "SDWebImageCompat.h"
|
#import "SDWebImageCompat.h"
|
||||||
|
|
||||||
#if SD_UIKIT || SD_MAC
|
|
||||||
|
|
||||||
#import "SDWebImageDefine.h"
|
#import "SDWebImageDefine.h"
|
||||||
#import "SDWebImageManager.h"
|
#import "SDWebImageManager.h"
|
||||||
#import "SDWebImageTransition.h"
|
#import "SDWebImageTransition.h"
|
||||||
|
@ -77,6 +74,8 @@ typedef void(^SDSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable ima
|
||||||
*/
|
*/
|
||||||
- (void)sd_cancelCurrentImageLoad;
|
- (void)sd_cancelCurrentImageLoad;
|
||||||
|
|
||||||
|
#if SD_UIKIT || SD_MAC
|
||||||
|
|
||||||
#pragma mark - Image Transition
|
#pragma mark - Image Transition
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -94,6 +93,6 @@ typedef void(^SDSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable ima
|
||||||
*/
|
*/
|
||||||
@property (nonatomic, strong, nullable) id<SDWebImageIndicator> sd_imageIndicator;
|
@property (nonatomic, strong, nullable) id<SDWebImageIndicator> sd_imageIndicator;
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
|
@ -7,9 +7,6 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#import "UIView+WebCache.h"
|
#import "UIView+WebCache.h"
|
||||||
|
|
||||||
#if SD_UIKIT || SD_MAC
|
|
||||||
|
|
||||||
#import "objc/runtime.h"
|
#import "objc/runtime.h"
|
||||||
#import "UIView+WebCacheOperation.h"
|
#import "UIView+WebCacheOperation.h"
|
||||||
#import "SDWebImageError.h"
|
#import "SDWebImageError.h"
|
||||||
|
@ -79,8 +76,11 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
|
||||||
self.sd_imageProgress.totalUnitCount = 0;
|
self.sd_imageProgress.totalUnitCount = 0;
|
||||||
self.sd_imageProgress.completedUnitCount = 0;
|
self.sd_imageProgress.completedUnitCount = 0;
|
||||||
|
|
||||||
|
#if SD_UIKIT || SD_MAC
|
||||||
// check and start image indicator
|
// check and start image indicator
|
||||||
[self sd_startImageIndicator];
|
[self sd_startImageIndicator];
|
||||||
|
id<SDWebImageIndicator> imageIndicator = self.sd_imageIndicator;
|
||||||
|
#endif
|
||||||
|
|
||||||
SDWebImageManager *manager;
|
SDWebImageManager *manager;
|
||||||
if ([context valueForKey:SDWebImageContextCustomManager]) {
|
if ([context valueForKey:SDWebImageContextCustomManager]) {
|
||||||
|
@ -89,21 +89,23 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
|
||||||
manager = [SDWebImageManager sharedManager];
|
manager = [SDWebImageManager sharedManager];
|
||||||
}
|
}
|
||||||
|
|
||||||
id<SDWebImageIndicator> imageIndicator = self.sd_imageIndicator;
|
|
||||||
|
|
||||||
__weak __typeof(self)wself = self;
|
__weak __typeof(self)wself = self;
|
||||||
SDWebImageDownloaderProgressBlock combinedProgressBlock = ^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
|
SDWebImageDownloaderProgressBlock combinedProgressBlock = ^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
|
||||||
wself.sd_imageProgress.totalUnitCount = expectedSize;
|
__strong __typeof (wself) sself = wself;
|
||||||
wself.sd_imageProgress.completedUnitCount = receivedSize;
|
NSProgress *imageProgress = sself.sd_imageProgress;
|
||||||
|
imageProgress.totalUnitCount = expectedSize;
|
||||||
|
imageProgress.completedUnitCount = receivedSize;
|
||||||
if (progressBlock) {
|
if (progressBlock) {
|
||||||
progressBlock(receivedSize, expectedSize, targetURL);
|
progressBlock(receivedSize, expectedSize, targetURL);
|
||||||
}
|
}
|
||||||
|
#if SD_UIKIT || SD_MAC
|
||||||
if ([imageIndicator respondsToSelector:@selector(updateIndicatorProgress:)]) {
|
if ([imageIndicator respondsToSelector:@selector(updateIndicatorProgress:)]) {
|
||||||
double progress = wself.sd_imageProgress.fractionCompleted;
|
double progress = imageProgress.fractionCompleted;
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
[imageIndicator updateIndicatorProgress:progress];
|
[imageIndicator updateIndicatorProgress:progress];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
id <SDWebImageOperation> operation = [manager loadImageWithURL:url options:options context:context progress:combinedProgressBlock completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
|
id <SDWebImageOperation> 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;
|
__strong __typeof (wself) sself = wself;
|
||||||
|
@ -114,10 +116,12 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
|
||||||
sself.sd_imageProgress.completedUnitCount = SDWebImageProgressUnitCountUnknown;
|
sself.sd_imageProgress.completedUnitCount = SDWebImageProgressUnitCountUnknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if SD_UIKIT || SD_MAC
|
||||||
// check and stop image indicator
|
// check and stop image indicator
|
||||||
if (finished) {
|
if (finished) {
|
||||||
[self sd_stopImageIndicator];
|
[self sd_stopImageIndicator];
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
BOOL shouldCallCompletedBlock = finished || (options & SDWebImageAvoidAutoSetImage);
|
BOOL shouldCallCompletedBlock = finished || (options & SDWebImageAvoidAutoSetImage);
|
||||||
BOOL shouldNotSetImage = ((image && (options & SDWebImageAvoidAutoSetImage)) ||
|
BOOL shouldNotSetImage = ((image && (options & SDWebImageAvoidAutoSetImage)) ||
|
||||||
|
@ -152,16 +156,22 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
|
||||||
targetData = nil;
|
targetData = nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if SD_UIKIT || SD_MAC
|
||||||
// check whether we should use the image transition
|
// check whether we should use the image transition
|
||||||
SDWebImageTransition *transition = nil;
|
SDWebImageTransition *transition = nil;
|
||||||
if (finished && (options & SDWebImageForceTransition || cacheType == SDImageCacheTypeNone)) {
|
if (finished && (options & SDWebImageForceTransition || cacheType == SDImageCacheTypeNone)) {
|
||||||
transition = sself.sd_imageTransition;
|
transition = sself.sd_imageTransition;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (group) {
|
if (group) {
|
||||||
dispatch_group_enter(group);
|
dispatch_group_enter(group);
|
||||||
dispatch_main_async_safe(^{
|
dispatch_main_async_safe(^{
|
||||||
|
#if SD_UIKIT || SD_MAC
|
||||||
[sself sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock transition:transition cacheType:cacheType imageURL:imageURL];
|
[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
|
// ensure completion block is called after custom setImage process finish
|
||||||
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
|
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
|
||||||
|
@ -169,14 +179,20 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
dispatch_main_async_safe(^{
|
dispatch_main_async_safe(^{
|
||||||
|
#if SD_UIKIT || SD_MAC
|
||||||
[sself sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock transition:transition cacheType:cacheType imageURL:imageURL];
|
[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();
|
callCompletedBlockClojure();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}];
|
}];
|
||||||
[self sd_setImageLoadOperation:operation forKey:validOperationKey];
|
[self sd_setImageLoadOperation:operation forKey:validOperationKey];
|
||||||
} else {
|
} else {
|
||||||
|
#if SD_UIKIT || SD_MAC
|
||||||
[self sd_stopImageIndicator];
|
[self sd_stopImageIndicator];
|
||||||
|
#endif
|
||||||
dispatch_main_async_safe(^{
|
dispatch_main_async_safe(^{
|
||||||
if (completedBlock) {
|
if (completedBlock) {
|
||||||
NSError *error = [NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorInvalidURL userInfo:@{NSLocalizedDescriptionKey : @"Image url is nil"}];
|
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 {
|
- (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];
|
[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 {
|
- (void)sd_setImage:(UIImage *)image imageData:(NSData *)imageData basedOnClassOrViaCustomSetImageBlock:(SDSetImageBlock)setImageBlock transition:(SDWebImageTransition *)transition cacheType:(SDImageCacheType)cacheType imageURL:(NSURL *)imageURL {
|
||||||
UIView *view = self;
|
UIView *view = self;
|
||||||
SDSetImageBlock finalSetImageBlock;
|
SDSetImageBlock finalSetImageBlock;
|
||||||
if (setImageBlock) {
|
if (setImageBlock) {
|
||||||
finalSetImageBlock = setImageBlock;
|
finalSetImageBlock = setImageBlock;
|
||||||
}
|
} else if ([view isKindOfClass:[UIImageView class]]) {
|
||||||
#if SD_UIKIT || SD_MAC
|
|
||||||
else if ([view isKindOfClass:[UIImageView class]]) {
|
|
||||||
UIImageView *imageView = (UIImageView *)view;
|
UIImageView *imageView = (UIImageView *)view;
|
||||||
finalSetImageBlock = ^(UIImage *setImage, NSData *setImageData) {
|
finalSetImageBlock = ^(UIImage *setImage, NSData *setImageData) {
|
||||||
imageView.image = setImage;
|
imageView.image = setImage;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
#if SD_UIKIT
|
#if SD_UIKIT
|
||||||
else if ([view isKindOfClass:[UIButton class]]) {
|
else if ([view isKindOfClass:[UIButton class]]) {
|
||||||
UIButton *button = (UIButton *)view;
|
UIButton *button = (UIButton *)view;
|
||||||
|
@ -273,15 +297,20 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
- (void)sd_setNeedsLayout {
|
- (void)sd_setNeedsLayout {
|
||||||
#if SD_UIKIT
|
#if SD_UIKIT
|
||||||
[self setNeedsLayout];
|
[self setNeedsLayout];
|
||||||
#elif SD_MAC
|
#elif SD_MAC
|
||||||
[self setNeedsLayout:YES];
|
[self setNeedsLayout:YES];
|
||||||
|
#elif SD_WATCH
|
||||||
|
// Do nothing because WatchKit automatically layout the view after property change
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if SD_UIKIT || SD_MAC
|
||||||
|
|
||||||
#pragma mark - Image Transition
|
#pragma mark - Image Transition
|
||||||
- (SDWebImageTransition *)sd_imageTransition {
|
- (SDWebImageTransition *)sd_imageTransition {
|
||||||
return objc_getAssociatedObject(self, @selector(sd_imageTransition));
|
return objc_getAssociatedObject(self, @selector(sd_imageTransition));
|
||||||
|
@ -340,6 +369,6 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
|
@ -7,10 +7,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#import "SDWebImageCompat.h"
|
#import "SDWebImageCompat.h"
|
||||||
|
#import "SDWebImageOperation.h"
|
||||||
#if SD_UIKIT || SD_MAC
|
|
||||||
|
|
||||||
#import "SDWebImageManager.h"
|
|
||||||
|
|
||||||
// These methods are used to support canceling for UIView image loading, it's designed to be used internal but not external.
|
// 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.
|
// 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;
|
- (void)sd_removeImageLoadOperationWithKey:(nullable NSString *)key;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
|
@ -7,9 +7,6 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#import "UIView+WebCacheOperation.h"
|
#import "UIView+WebCacheOperation.h"
|
||||||
|
|
||||||
#if SD_UIKIT || SD_MAC
|
|
||||||
|
|
||||||
#import "objc/runtime.h"
|
#import "objc/runtime.h"
|
||||||
|
|
||||||
static char loadOperationKey;
|
static char loadOperationKey;
|
||||||
|
@ -80,5 +77,3 @@ typedef NSMapTable<NSString *, id<SDWebImageOperation>> SDOperationsDictionary;
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
Loading…
Reference in New Issue