iOS:update SDWebImage API call

This commit is contained in:
pengfeizhou 2021-01-29 10:27:28 +08:00 committed by osborn
parent 4ad278e9c9
commit 4c78a91f7a
3 changed files with 211 additions and 43 deletions

View File

@ -23,12 +23,166 @@ class ImageDemo extends Panel {
textAlignment: gravity().center(), textAlignment: gravity().center(),
height: 50, height: 50,
}), }),
label('Button'),
image({
imageBase64: button,
scaleType: ScaleType.ScaleToFill,
layoutConfig: {
widthSpec: LayoutSpec.FIT,
heightSpec: LayoutSpec.FIT,
},
imageScale: 2,
}),
image({
imageBase64: button,
scaleType: ScaleType.ScaleToFill,
layoutConfig: {
widthSpec: LayoutSpec.FIT,
heightSpec: LayoutSpec.FIT,
},
}),
image({
imageBase64: button,
scaleType: ScaleType.ScaleToFill,
layoutConfig: {
widthSpec: LayoutSpec.JUST,
heightSpec: LayoutSpec.JUST,
},
width: 200,
height: 150 / 2.75,
stretchInset: {
left: 100,
top: 0,
right: 100,
bottom: 0
},
imageScale: 2.75,
}),
image({
imageBase64: button,
scaleType: ScaleType.ScaleToFill,
layoutConfig: {
widthSpec: LayoutSpec.JUST,
heightSpec: LayoutSpec.JUST,
},
width: 200,
height: 75,
stretchInset: {
left: 100,
top: 0,
right: 100,
bottom: 0
},
imageScale: 2,
}),
label('Gif'),
image({
imageUrl: "https://misc.aotu.io/ONE-SUNDAY/world-cup_2014_42.gif",
scaleType: ScaleType.ScaleToFill,
loadCallback: function (ret) {
log('this')
log('loadCallback', ret)
},
imageScale: 2,
}),
label('APNG'),
image({
imageUrl: "https://misc.aotu.io/ONE-SUNDAY/world_cup_2014_42.png",
loadCallback: (ret) => {
}
}),
label('Animated WebP'), label('Animated WebP'),
image({ image({
imageUrl: "https://p.upyun.com/demo/webp/webp/animated-gif-0.webp", imageUrl: "https://p.upyun.com/demo/webp/webp/animated-gif-0.webp",
loadCallback: (ret) => { loadCallback: (ret) => {
} }
}), }),
label('WebP'),
imageView = image({
imageUrl: "https://p.upyun.com/demo/webp/webp/jpg-0.webp",
layoutConfig: layoutConfig().just(),
width: 200,
height: 200,
// loadCallback: (ret) => {
// if (ret) {
// imageView.width = ret.width
// imageView.height = ret.height
// }
// }
}),
label('ScaleToFill'),
image({
imageUrl,
width: 300,
height: 300,
isBlur: true,
border: {
width: 2,
color: Color.GRAY,
},
scaleType: ScaleType.ScaleToFill,
layoutConfig: layoutConfig().just(),
loadCallback: (ret) => {
}
}),
label('ScaleAspectFit'),
image({
imageUrl,
width: 300,
height: 300,
border: {
width: 2,
color: Color.GRAY,
},
scaleType: ScaleType.ScaleAspectFit,
layoutConfig: layoutConfig().just(),
}),
label('ScaleAspectFill'),
image({
imageUrl,
width: 300,
height: 300,
border: {
width: 2,
color: Color.GRAY,
},
scaleType: ScaleType.ScaleAspectFill,
layoutConfig: layoutConfig().just(),
}),
label('ImageBase64'),
image({
imageBase64: img_base64[0],
width: 300,
height: 300,
border: {
width: 2,
color: Color.GRAY,
},
scaleType: ScaleType.ScaleAspectFill,
layoutConfig: layoutConfig().just(),
}),
label('StretchInset'),
image({
imageBase64: img_base64[1],
height: 60,
width: 134,
scaleType: ScaleType.ScaleAspectFill,
layoutConfig: layoutConfig().just(),
}),
image({
imageBase64: img_base64[1],
height: 60,
width: 294,
scaleType: ScaleType.ScaleToFill,
layoutConfig: layoutConfig().just(),
stretchInset: {
left: 0.85,
top: 0,
right: 0.149,
bottom: 0
}
}),
], ],
{ {
layoutConfig: layoutConfig().most().configHeight(LayoutSpec.FIT), layoutConfig: layoutConfig().most().configHeight(LayoutSpec.FIT),
@ -70,4 +224,4 @@ class ImageDemo extends Panel {
onDestroy() { onDestroy() {
modal(context).toast('onDestroy') modal(context).toast('onDestroy')
} }
} }

View File

@ -9,6 +9,8 @@
#import "AppDelegate.h" #import "AppDelegate.h"
#import "NavigationController.h" #import "NavigationController.h"
#import "ViewController.h" #import "ViewController.h"
#import <SDWebImage/SDWebImage.h>
#import <SDWebImageWebPCoder/SDWebImageWebPCoder.h>
@interface AppDelegate () @interface AppDelegate ()
@property(nonatomic, strong) UIViewController *rootVC; @property(nonatomic, strong) UIViewController *rootVC;
@ -28,6 +30,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
self.window.rootViewController = self.navigationController; self.window.rootViewController = self.navigationController;
[self.window addSubview:self.navigationController.view]; [self.window addSubview:self.navigationController.view];
[self.window makeKeyAndVisible]; [self.window makeKeyAndVisible];
[SDImageCodersManager.sharedManager addCoder:SDImageWebPCoder.sharedCoder];
return YES; return YES;
} }

View File

@ -130,7 +130,10 @@ - (UIImage *)currentPlaceHolderImage {
#if __has_include(<YYWebImage/YYWebImage.h>) #if __has_include(<YYWebImage/YYWebImage.h>)
YYImage *image = [YYImage imageWithData:imageData scale:self.imageScale]; YYImage *image = [YYImage imageWithData:imageData scale:self.imageScale];
#elif __has_include(<SDWebImage/SDWebImage.h>) #elif __has_include(<SDWebImage/SDWebImage.h>)
SDAnimatedImage *image = [SDAnimatedImage imageWithData:imageData scale:self.imageScale]; UIImage *image = [SDAnimatedImage imageWithData:imageData scale:self.imageScale];
if (!image) {
image = [UIImage imageWithData:imageData scale:self.imageScale];
}
#else #else
UIImage *image = [UIImage imageWithData:imageData scale:self.imageScale]; UIImage *image = [UIImage imageWithData:imageData scale:self.imageScale];
#endif #endif
@ -169,7 +172,10 @@ - (UIImage *)currentErrorImage {
#if __has_include(<YYWebImage/YYWebImage.h>) #if __has_include(<YYWebImage/YYWebImage.h>)
YYImage *image = [YYImage imageWithData:imageData scale:self.imageScale]; YYImage *image = [YYImage imageWithData:imageData scale:self.imageScale];
#elif __has_include(<SDWebImage/SDWebImage.h>) #elif __has_include(<SDWebImage/SDWebImage.h>)
SDAnimatedImage *image = [SDAnimatedImage imageWithData:imageData scale:self.imageScale]; UIImage *image = [SDAnimatedImage imageWithData:imageData scale:self.imageScale];
if (!image) {
image = [UIImage imageWithData:imageData scale:self.imageScale];
}
#else #else
UIImage *image = [UIImage imageWithData:imageData scale:self.imageScale]; UIImage *image = [UIImage imageWithData:imageData scale:self.imageScale];
#endif #endif
@ -232,43 +238,39 @@ - (void)blendView:(UIImageView *)view forPropName:(NSString *)name propValue:(id
} }
}]; }];
#elif __has_include(<SDWebImage/SDWebImage.h>) #elif __has_include(<SDWebImage/SDWebImage.h>)
[view sd_setImageWithURL:[NSURL URLWithString:prop] placeholderImage:[self currentPlaceHolderImage] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { [view sd_setImageWithURL:[NSURL URLWithString:prop]
__strong typeof(_self) self = _self; placeholderImage:[self currentPlaceHolderImage]
if (self.placeHolderColor || self.errorColor) { options:0
self.view.contentMode = self.contentMode; context:@{SDWebImageContextImageScaleFactor: @(self.imageScale)}
} progress:nil
view.doricLayout.undefined = NO; completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (error) { __strong typeof(_self) self = _self;
[[self currentErrorImage] also:^(UIImage *it) { if (self.placeHolderColor || self.errorColor) {
self.view.image = it; self.view.contentMode = self.contentMode;
}]; }
if (self.loadCallbackId.length > 0) { view.doricLayout.undefined = NO;
[self callJSResponse:self.loadCallbackId, nil]; if (error) {
} [[self currentErrorImage] also:^(UIImage *it) {
} else { self.view.image = it;
if (image.scale != self.imageScale) { }];
if ([image isKindOfClass:SDAnimatedImage.class]) { if (self.loadCallbackId.length > 0) {
image = [SDAnimatedImage imageWithData:((SDAnimatedImage *) image).animatedImageData scale:self.imageScale]; [self callJSResponse:self.loadCallbackId, nil];
} else { }
image = [UIImage imageWithCGImage:image.CGImage scale:self.imageScale orientation:image.imageOrientation]; } else {
} if (self.loadCallbackId.length > 0) {
self.view.image = image; [self callJSResponse:self.loadCallbackId,
} @{@"width": @(image.size.width), @"height": @(image.size.height)},
if (self.loadCallbackId.length > 0) { nil];
[self callJSResponse:self.loadCallbackId, }
@{@"width": @(image.size.width), @"height": @(image.size.height)}, if (async) {
nil]; DoricSuperNode *node = self.superNode;
} while (node.superNode != nil) {
if (async) { node = node.superNode;
DoricSuperNode *node = self.superNode; }
while (node.superNode != nil) { [node requestLayout];
node = node.superNode; }
} }
[node requestLayout]; }];
}
}
}];
#else #else
DoricLog(@"Do not support load image url"); DoricLog(@"Do not support load image url");
#endif #endif
@ -301,7 +303,10 @@ - (void)blendView:(UIImageView *)view forPropName:(NSString *)name propValue:(id
#if __has_include(<YYWebImage/YYWebImage.h>) #if __has_include(<YYWebImage/YYWebImage.h>)
YYImage *image = [YYImage imageWithData:imageData scale:self.imageScale]; YYImage *image = [YYImage imageWithData:imageData scale:self.imageScale];
#elif __has_include(<SDWebImage/SDWebImage.h>) #elif __has_include(<SDWebImage/SDWebImage.h>)
SDAnimatedImage *image = [SDAnimatedImage imageWithData:imageData scale:self.imageScale]; UIImage *image = [SDAnimatedImage imageWithData:imageData scale:self.imageScale];
if (!image) {
image = [UIImage imageWithData:imageData scale:self.imageScale];
}
#else #else
UIImage *image = [UIImage imageWithData:imageData scale:self.imageScale]; UIImage *image = [UIImage imageWithData:imageData scale:self.imageScale];
#endif #endif
@ -340,7 +345,10 @@ - (void)blendView:(UIImageView *)view forPropName:(NSString *)name propValue:(id
#if __has_include(<YYWebImage/YYWebImage.h>) #if __has_include(<YYWebImage/YYWebImage.h>)
YYImage *image = [YYImage imageNamed:prop]; YYImage *image = [YYImage imageNamed:prop];
#elif __has_include(<SDWebImage/SDWebImage.h>) #elif __has_include(<SDWebImage/SDWebImage.h>)
SDAnimatedImage *image = [SDAnimatedImage imageNamed:prop]; UIImage *image = [SDAnimatedImage imageNamed:prop];
if (!image) {
image = [UIImage imageNamed:prop];
}
#else #else
UIImage *image = [UIImage imageNamed:prop]; UIImage *image = [UIImage imageNamed:prop];
#endif #endif
@ -367,7 +375,10 @@ - (void)blendView:(UIImageView *)view forPropName:(NSString *)name propValue:(id
#if __has_include(<YYWebImage/YYWebImage.h>) #if __has_include(<YYWebImage/YYWebImage.h>)
YYImage *image = [YYImage imageWithData:imgData scale:self.imageScale]; YYImage *image = [YYImage imageWithData:imgData scale:self.imageScale];
#elif __has_include(<SDWebImage/SDWebImage.h>) #elif __has_include(<SDWebImage/SDWebImage.h>)
SDAnimatedImage *image = [SDAnimatedImage imageWithData:imgData scale:self.imageScale]; UIImage *image = [SDAnimatedImage imageWithData:imgData scale:self.imageScale];
if (!image) {
image = [UIImage imageWithData:imgData scale:self.imageScale];
}
#else #else
UIImage *image = [UIImage imageWithData:imgData scale:self.imageScale]; UIImage *image = [UIImage imageWithData:imgData scale:self.imageScale];
#endif #endif