From e2ca9062283d9768dfdd68f259f7b9fb3f98f6bf Mon Sep 17 00:00:00 2001 From: "qianyuan.wqy" Date: Mon, 30 Sep 2019 16:11:58 +0800 Subject: [PATCH] Revert "[iOS] Fix problem that _backgroundColor causing multithread crashing. (#2934)" This reverts commit 87454ac332908c2d21bebc694e8db0538b05a3a0. --- .../Sources/Component/WXComponent_internal.h | 10 +-- .../Sources/Display/WXComponent+Display.m | 61 ++++--------------- .../WeexSDK/Sources/Loader/WXResourceLoader.m | 9 ++- ios/sdk/WeexSDK/Sources/Model/WXComponent.mm | 9 ++- ios/sdk/WeexSDK/Sources/Utility/WXConvert.h | 2 - ios/sdk/WeexSDK/Sources/Utility/WXConvert.m | 30 --------- .../View/WXComponent+ViewManagement.mm | 13 ++-- 7 files changed, 36 insertions(+), 98 deletions(-) diff --git a/ios/sdk/WeexSDK/Sources/Component/WXComponent_internal.h b/ios/sdk/WeexSDK/Sources/Component/WXComponent_internal.h index e124c5bc18..c58a7967d5 100644 --- a/ios/sdk/WeexSDK/Sources/Component/WXComponent_internal.h +++ b/ios/sdk/WeexSDK/Sources/Component/WXComponent_internal.h @@ -49,7 +49,7 @@ typedef id (^WXDataBindingBlock)(NSDictionary *data, BOOL *needUpdate); /** * View */ - uint32_t _backgroundColor; + UIColor *_backgroundColor; NSString *_backgroundImage; NSString *_clipRadius; WXClipType _clipToBounds; @@ -114,10 +114,10 @@ typedef id (^WXDataBindingBlock)(NSDictionary *data, BOOL *needUpdate); BOOL _isCompositingChild; WXThreadSafeCounter *_displayCounter; - uint32_t _borderTopColor; - uint32_t _borderRightColor; - uint32_t _borderLeftColor; - uint32_t _borderBottomColor; + UIColor *_borderTopColor; + UIColor *_borderRightColor; + UIColor *_borderLeftColor; + UIColor *_borderBottomColor; CGFloat _borderTopWidth; CGFloat _borderRightWidth; diff --git a/ios/sdk/WeexSDK/Sources/Display/WXComponent+Display.m b/ios/sdk/WeexSDK/Sources/Display/WXComponent+Display.m index c4eb81f25e..9acb01089c 100644 --- a/ios/sdk/WeexSDK/Sources/Display/WXComponent+Display.m +++ b/ios/sdk/WeexSDK/Sources/Display/WXComponent+Display.m @@ -28,7 +28,6 @@ #import "UIBezierPath+Weex.h" #import "WXRoundedRect.h" #import "WXSDKInstance.h" -#import "WXConvert.h" #pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation" @@ -290,7 +289,7 @@ - (WXDisplayBlock)_compositeDisplayBlock - (void)_collectCompositingDisplayBlocks:(NSMutableArray *)displayBlocks context:(CGContextRef)context isCancelled:(BOOL(^)(void))isCancelled { // TODO: compositingChild has no chance to applyPropertiesToView, need update here? - UIColor *backgroundColor = [WXConvert UIColorFromRGBA:_backgroundColor]; + UIColor *backgroundColor = _backgroundColor; BOOL clipsToBounds = _clipToBounds; CGRect frame = self.calculatedFrame; CGRect bounds = CGRectMake(0, 0, frame.size.width, frame.size.height); @@ -350,9 +349,8 @@ - (void)_drawBorderWithContext:(CGContextRef)context size:(CGSize)size CGContextSetAlpha(context, _opacity); // fill background color - UIColor* backgroundColor = [WXConvert UIColorFromRGBA:_backgroundColor]; - if (CGColorGetAlpha(backgroundColor.CGColor) > 0) { - CGContextSetFillColorWithColor(context, backgroundColor.CGColor); + if (_backgroundColor && CGColorGetAlpha(_backgroundColor.CGColor) > 0) { + CGContextSetFillColorWithColor(context, _backgroundColor.CGColor); UIBezierPath *bezierPath = [UIBezierPath wx_bezierPathWithRoundedRect:rect topLeft:topLeft topRight:topRight bottomLeft:bottomLeft bottomRight:bottomRight]; [bezierPath fill]; WXPerformBlockOnMainThread(^{ @@ -369,7 +367,7 @@ - (void)_drawBorderWithContext:(CGContextRef)context size:(CGSize)size CGContextSetLineDash(context, 0, 0, 0); } CGContextSetLineWidth(context, _borderTopWidth); - CGContextSetStrokeColorWithColor(context, [WXConvert UIColorFromRGBA:_borderTopColor].CGColor); + CGContextSetStrokeColorWithColor(context, _borderTopColor.CGColor); CGContextAddArc(context, size.width-topRight, topRight, topRight-_borderTopWidth/2, -M_PI_4+(_borderRightWidth>0?0:M_PI_4), -M_PI_2, 1); CGContextMoveToPoint(context, size.width-topRight, _borderTopWidth/2); CGContextAddLineToPoint(context, topLeft, _borderTopWidth/2); @@ -390,7 +388,7 @@ - (void)_drawBorderWithContext:(CGContextRef)context size:(CGSize)size CGContextSetLineDash(context, 0, 0, 0); } CGContextSetLineWidth(context, _borderLeftWidth); - CGContextSetStrokeColorWithColor(context, [WXConvert UIColorFromRGBA:_borderLeftColor].CGColor); + CGContextSetStrokeColorWithColor(context, _borderLeftColor.CGColor); CGContextAddArc(context, topLeft, topLeft, topLeft-_borderLeftWidth/2, -M_PI, -M_PI_2-M_PI_4+(_borderTopWidth > 0?0:M_PI_4), 0); CGContextMoveToPoint(context, _borderLeftWidth/2, topLeft); CGContextAddLineToPoint(context, _borderLeftWidth/2, size.height-bottomLeft); @@ -411,7 +409,7 @@ - (void)_drawBorderWithContext:(CGContextRef)context size:(CGSize)size CGContextSetLineDash(context, 0, 0, 0); } CGContextSetLineWidth(context, _borderBottomWidth); - CGContextSetStrokeColorWithColor(context, [WXConvert UIColorFromRGBA:_borderBottomColor].CGColor); + CGContextSetStrokeColorWithColor(context, _borderBottomColor.CGColor); CGContextAddArc(context, bottomLeft, size.height-bottomLeft, bottomLeft-_borderBottomWidth/2, M_PI-M_PI_4+(_borderLeftWidth>0?0:M_PI_4), M_PI_2, 1); CGContextMoveToPoint(context, bottomLeft, size.height-_borderBottomWidth/2); CGContextAddLineToPoint(context, size.width-bottomRight, size.height-_borderBottomWidth/2); @@ -432,7 +430,7 @@ - (void)_drawBorderWithContext:(CGContextRef)context size:(CGSize)size CGContextSetLineDash(context, 0, 0, 0); } CGContextSetLineWidth(context, _borderRightWidth); - CGContextSetStrokeColorWithColor(context, [WXConvert UIColorFromRGBA:_borderRightColor].CGColor); + CGContextSetStrokeColorWithColor(context, _borderRightColor.CGColor); CGContextAddArc(context, size.width-bottomRight, size.height-bottomRight, bottomRight-_borderRightWidth/2, M_PI_4+(_borderBottomWidth>0?0:M_PI_4), 0, 1); CGContextMoveToPoint(context, size.width-_borderRightWidth/2, size.height-bottomRight); CGContextAddLineToPoint(context, size.width-_borderRightWidth/2, topRight); @@ -488,7 +486,7 @@ - (BOOL)_needsDrawBorder if (!radiusEqual) { return YES; } - BOOL colorEqual = _borderTopColor == _borderRightColor && _borderRightColor == _borderBottomColor && _borderBottomColor == _borderLeftColor; + BOOL colorEqual = [_borderTopColor isEqual:_borderRightColor] && [_borderRightColor isEqual:_borderBottomColor] && [_borderBottomColor isEqual:_borderLeftColor]; if (!colorEqual) { return YES; } @@ -501,7 +499,7 @@ - (void)_handleBorders:(NSDictionary *)styles isUpdating:(BOOL)updating if (!updating) { // init with default value _borderTopStyle = _borderRightStyle = _borderBottomStyle = _borderLeftStyle = WXBorderStyleSolid; - _borderTopColor = _borderLeftColor = _borderRightColor = _borderBottomColor = [WXConvert RGBAColorFromUIColor:[UIColor blackColor]]; + _borderTopColor = _borderLeftColor = _borderRightColor = _borderBottomColor = [UIColor blackColor]; _borderTopWidth = _borderLeftWidth = _borderRightWidth = _borderBottomWidth = 0; _borderTopLeftRadius = _borderTopRightRadius = _borderBottomLeftRadius = _borderBottomRightRadius = 0; } @@ -543,39 +541,6 @@ - (void)_handleBorders:(NSDictionary *)styles isUpdating:(BOOL)updating [self setNeedsDisplay];\ }\ } while (0); - -#define WX_CHECK_BORDER_PROP_COLOR(prop, direction1, direction2, direction3, direction4, type)\ -do {\ - BOOL needsDisplay = NO; \ - NSString *styleProp= WX_NSSTRING(WX_CONCAT(border, prop));\ - if (styles[styleProp]) {\ - _border##direction1##prop = _border##direction2##prop = _border##direction3##prop = _border##direction4##prop = [WXConvert RGBAColorFromUIColor:[WXConvert type:styles[styleProp]]];\ - needsDisplay = YES;\ - }\ - NSString *styleDirection1Prop = WX_NSSTRING(WX_CONCAT_TRIPLE(border, direction1, prop));\ - if (styles[styleDirection1Prop]) {\ - _border##direction1##prop = [WXConvert RGBAColorFromUIColor:[WXConvert type:styles[styleDirection1Prop]]];\ - needsDisplay = YES;\ - }\ - NSString *styleDirection2Prop = WX_NSSTRING(WX_CONCAT_TRIPLE(border, direction2, prop));\ - if (styles[styleDirection2Prop]) {\ - _border##direction2##prop = [WXConvert RGBAColorFromUIColor:[WXConvert type:styles[styleDirection2Prop]]];\ - needsDisplay = YES;\ - }\ - NSString *styleDirection3Prop = WX_NSSTRING(WX_CONCAT_TRIPLE(border, direction3, prop));\ - if (styles[styleDirection3Prop]) {\ - _border##direction3##prop = [WXConvert RGBAColorFromUIColor:[WXConvert type:styles[styleDirection3Prop]]];\ - needsDisplay = YES;\ - }\ - NSString *styleDirection4Prop = WX_NSSTRING(WX_CONCAT_TRIPLE(border, direction4, prop));\ - if (styles[styleDirection4Prop]) {\ - _border##direction4##prop = [WXConvert RGBAColorFromUIColor:[WXConvert type:styles[styleDirection4Prop]]];\ - needsDisplay = YES;\ - }\ - if (needsDisplay && updating) {\ - [self setNeedsDisplay];\ - }\ -} while (0); // TODO: refactor this hopefully #define WX_CHECK_BORDER_PROP_PIXEL(prop, direction1, direction2, direction3, direction4)\ @@ -613,7 +578,7 @@ - (void)_handleBorders:(NSDictionary *)styles isUpdating:(BOOL)updating WX_CHECK_BORDER_PROP(Style, Top, Left, Bottom, Right, WXBorderStyle) - WX_CHECK_BORDER_PROP_COLOR(Color, Top, Left, Bottom, Right, UIColor) + WX_CHECK_BORDER_PROP(Color, Top, Left, Bottom, Right, UIColor) WX_CHECK_BORDER_PROP_PIXEL(Width, Top, Left, Bottom, Right) WX_CHECK_BORDER_PROP_PIXEL(Radius, TopLeft, TopRight, BottomLeft, BottomRight) @@ -627,9 +592,9 @@ - (void)_handleBorders:(NSDictionary *)styles isUpdating:(BOOL)updating } else if (!nowNeedsDrawBorder) { [self _resetNativeBorderRadius]; _layer.borderWidth = _borderTopWidth; - _layer.borderColor = [WXConvert UIColorFromRGBA:_borderTopColor].CGColor; + _layer.borderColor = _borderTopColor.CGColor; if ((_transition.transitionOptions & WXTransitionOptionsBackgroundColor) != WXTransitionOptionsBackgroundColor ) { - _layer.backgroundColor = [WXConvert UIColorFromRGBA:_backgroundColor].CGColor; + _layer.backgroundColor = _backgroundColor.CGColor; } } } @@ -641,7 +606,7 @@ - (BOOL)_bitmapOpaqueWithSize:(CGSize)size WXRoundedRect *borderRect = [[WXRoundedRect alloc] initWithRect:rect topLeft:_borderTopLeftRadius topRight:_borderTopRightRadius bottomLeft:_borderBottomLeftRadius bottomRight:_borderBottomRightRadius]; WXRadii *radii = borderRect.radii; BOOL hasBorderRadius = [radii hasBorderRadius]; - return (!hasBorderRadius) && _opacity == 1.0 && CGColorGetAlpha([WXConvert UIColorFromRGBA:_backgroundColor].CGColor) == 1.0 && [self _needsDrawBorder]; + return (!hasBorderRadius) && _opacity == 1.0 && CGColorGetAlpha(_backgroundColor.CGColor) == 1.0 && [self _needsDrawBorder]; } - (CAShapeLayer *)drawBorderRadiusMaskLayer:(CGRect)rect diff --git a/ios/sdk/WeexSDK/Sources/Loader/WXResourceLoader.m b/ios/sdk/WeexSDK/Sources/Loader/WXResourceLoader.m index 8423f78cd6..9015625900 100644 --- a/ios/sdk/WeexSDK/Sources/Loader/WXResourceLoader.m +++ b/ios/sdk/WeexSDK/Sources/Loader/WXResourceLoader.m @@ -140,7 +140,14 @@ - (void)request:(WXResourceRequest *)request didReceiveResponse:(WXResourceRespo WXLogDebug(@"request:%@ didReceiveResponse:%@ ", request, response); _response = response; - _data = nil; + id configCenter = [WXSDKEngine handlerForProtocol:@protocol(WXConfigCenterProtocol)]; + if ([configCenter respondsToSelector:@selector(configForKey:defaultValue:isDefault:)]) { + BOOL isDefault; + BOOL clearResponseData = [[configCenter configForKey:@"iOS_weex_ext_config.clearResponseDataWhenDidReceiveResponse" defaultValue:@(NO) isDefault:&isDefault] boolValue]; + if(clearResponseData) { + _data = nil; + } + } if (self.onResponseReceived) { self.onResponseReceived(response); diff --git a/ios/sdk/WeexSDK/Sources/Model/WXComponent.mm b/ios/sdk/WeexSDK/Sources/Model/WXComponent.mm index 9c99ac9959..a60251cb9a 100644 --- a/ios/sdk/WeexSDK/Sources/Model/WXComponent.mm +++ b/ios/sdk/WeexSDK/Sources/Model/WXComponent.mm @@ -387,11 +387,11 @@ - (UIView *)view _view.hidden = _visibility == WXVisibilityShow ? NO : YES; _view.clipsToBounds = _clipToBounds; if (![self _needsDrawBorder]) { - _layer.borderColor = [WXConvert UIColorFromRGBA:_borderTopColor].CGColor; + _layer.borderColor = _borderTopColor.CGColor; _layer.borderWidth = _borderTopWidth; [self _resetNativeBorderRadius]; _layer.opacity = _opacity; - _view.backgroundColor = [WXConvert UIColorFromRGBA:_backgroundColor]; + _view.backgroundColor = _backgroundColor; } if (_backgroundImage) { @@ -857,9 +857,8 @@ - (void)setGradientLayer UIColor * endColor = (UIColor*)linearGradient[@"endColor"]; CAGradientLayer * gradientLayer = [WXUtility gradientLayerFromColors:@[startColor, endColor] locations:nil frame:strongSelf.view.bounds gradientType:(WXGradientType)[linearGradient[@"gradientType"] integerValue]]; if (gradientLayer) { - UIColor* patternColor = [UIColor colorWithPatternImage:[strongSelf imageFromLayer:gradientLayer]]; - _backgroundColor = [WXConvert RGBAColorFromUIColor:patternColor]; - strongSelf.view.backgroundColor = patternColor; + _backgroundColor = [UIColor colorWithPatternImage:[strongSelf imageFromLayer:gradientLayer]]; + strongSelf.view.backgroundColor = _backgroundColor; [strongSelf setNeedsDisplay]; } } diff --git a/ios/sdk/WeexSDK/Sources/Utility/WXConvert.h b/ios/sdk/WeexSDK/Sources/Utility/WXConvert.h index 187da8bf9b..c57f4b1e6b 100644 --- a/ios/sdk/WeexSDK/Sources/Utility/WXConvert.h +++ b/ios/sdk/WeexSDK/Sources/Utility/WXConvert.h @@ -64,8 +64,6 @@ typedef CGFloat WXPixelType; + (UIAccessibilityTraits)WXUIAccessibilityTraits:(id)value; + (UIColor *)UIColor:(id)value; -+ (UIColor *)UIColorFromRGBA:(uint32_t)rgba; -+ (uint32_t)RGBAColorFromUIColor:(UIColor*)color; + (CGColorRef)CGColor:(id)value; + (NSString *)HexWithColor:(UIColor *)color; + (WXBorderStyle)WXBorderStyle:(id)value; diff --git a/ios/sdk/WeexSDK/Sources/Utility/WXConvert.m b/ios/sdk/WeexSDK/Sources/Utility/WXConvert.m index 6861b0873b..85ab360a6c 100644 --- a/ios/sdk/WeexSDK/Sources/Utility/WXConvert.m +++ b/ios/sdk/WeexSDK/Sources/Utility/WXConvert.m @@ -428,36 +428,6 @@ + (UIColor *)UIColor:(id)value return color; } -+ (UIColor *)UIColorFromRGBA:(uint32_t)rgba -{ - float r, g, b, a; - uint8_t r8, g8, b8, a8; - r8 = rgba >> 24; - g8 = (rgba & 0x00FF0000) >> 16; - b8 = (rgba & 0x0000FF00) >> 8; - a8 = rgba & 0x000000FF; - r = r8 / 255.f; - g = g8 / 255.f; - b = b8 / 255.f; - a = a8 / 255.f; - return [UIColor colorWithRed:r green:g blue:b alpha:a]; -} - -+ (uint32_t)RGBAColorFromUIColor:(UIColor*)color -{ - CGFloat r, g, b, a; - if (color) { - [color getRed:&r green:&g blue:&b alpha:&a]; - uint8_t r8, g8, b8, a8; - r8 = (uint8_t)(lround(r * 255.f)); - g8 = (uint8_t)(lround(g * 255.f)); - b8 = (uint8_t)(lround(b * 255.f)); - a8 = (uint8_t)(lround(a * 255.f)); - return ((uint32_t)r8 << 24) | ((uint32_t)g8 << 16) | ((uint32_t)b8 << 8) | (uint32_t)a8; - } - return 0; -} - + (CGColorRef)CGColor:(id)value { UIColor *color = [self UIColor:value]; diff --git a/ios/sdk/WeexSDK/Sources/View/WXComponent+ViewManagement.mm b/ios/sdk/WeexSDK/Sources/View/WXComponent+ViewManagement.mm index 78f8cb2215..3d97548a83 100644 --- a/ios/sdk/WeexSDK/Sources/View/WXComponent+ViewManagement.mm +++ b/ios/sdk/WeexSDK/Sources/View/WXComponent+ViewManagement.mm @@ -62,7 +62,7 @@ #define WX_BOARD_RADIUS_COLOR_RESET_ALL(key)\ do {\ if (styles && [styles containsObject:@#key]) {\ - _borderTopColor = _borderLeftColor = _borderRightColor = _borderBottomColor = [WXConvert RGBAColorFromUIColor:[UIColor blackColor]];\ + _borderTopColor = _borderLeftColor = _borderRightColor = _borderBottomColor = [UIColor blackColor];\ [self setNeedsDisplay];\ }\ } while(0); @@ -70,7 +70,7 @@ #define WX_BOARD_COLOR_RESET(key)\ do {\ if (styles && [styles containsObject:@#key]) {\ - _##key = [WXConvert RGBAColorFromUIColor:[UIColor blackColor]];\ + _##key = [UIColor blackColor];\ [self setNeedsDisplay];\ }\ } while(0); @@ -174,8 +174,7 @@ - (void)viewDidUnload - (void)_initViewPropertyWithStyles:(NSDictionary *)styles { - UIColor* bgColor = styles[@"backgroundColor"] ? [WXConvert UIColor:styles[@"backgroundColor"]] : [UIColor clearColor]; - _backgroundColor = [WXConvert RGBAColorFromUIColor:bgColor ?: [UIColor clearColor]]; + _backgroundColor = styles[@"backgroundColor"] ? [WXConvert UIColor:styles[@"backgroundColor"]] : [UIColor clearColor]; _backgroundImage = styles[@"backgroundImage"] ? [WXConvert NSString:styles[@"backgroundImage"]]: nil; _opacity = styles[@"opacity"] ? [WXConvert CGFloat:styles[@"opacity"]] : 1.0; _clipToBounds = styles[@"overflow"] ? [WXConvert WXClipType:styles[@"overflow"]] : NO; @@ -194,7 +193,7 @@ - (void)_transitionUpdateViewProperty:(NSDictionary *)styles { WX_CHECK_COMPONENT_TYPE(self.componentType) if (styles[@"backgroundColor"]) { - _backgroundColor = [WXConvert RGBAColorFromUIColor:[WXConvert UIColor:styles[@"backgroundColor"]]]; + _backgroundColor = [WXConvert UIColor:styles[@"backgroundColor"]]; } if (styles[@"opacity"]) { _opacity = [WXConvert CGFloat:styles[@"opacity"]]; @@ -212,7 +211,7 @@ - (void)_updateViewStyles:(NSDictionary *)styles } if (styles[@"backgroundColor"]) { - _backgroundColor = [WXConvert RGBAColorFromUIColor:[WXConvert UIColor:styles[@"backgroundColor"]]]; + _backgroundColor = [WXConvert UIColor:styles[@"backgroundColor"]]; [self setNeedsDisplay]; } @@ -308,7 +307,7 @@ - (void)resetBorder:(NSArray *)styles - (void)_resetStyles:(NSArray *)styles { if (styles && [styles containsObject:@"backgroundColor"]) { - _backgroundColor = [WXConvert RGBAColorFromUIColor:[UIColor clearColor]]; + _backgroundColor = [UIColor clearColor]; [self setNeedsDisplay]; } if (styles && [styles containsObject:@"boxShadow"]) {