From 48573efbcf52e32045f693eb4dcd2830b7ea8206 Mon Sep 17 00:00:00 2001 From: wwwcg Date: Fri, 10 May 2024 11:25:19 +0800 Subject: [PATCH] fix(ios): avoid zero size crash when using UIGraphics api in debug mode --- ios/sdk/component/view/HippyView.m | 5 +++++ ios/sdk/debug/devtools/inspector/model/HippyPageModel.m | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/ios/sdk/component/view/HippyView.m b/ios/sdk/component/view/HippyView.m index f744b5f6ea5..e846b9ad483 100644 --- a/ios/sdk/component/view/HippyView.m +++ b/ios/sdk/component/view/HippyView.m @@ -642,6 +642,11 @@ - (BOOL)getLayerContentForColor:(UIColor *)color completionBlock:(void (^)(UIIma return YES; } else if (self.backgroundImageUrl) { + CGSize size = theFrame.size; + if (0 >= size.width || 0 >= size.height) { + contentBlock(nil); + return YES; + } CGFloat backgroundPositionX = self.backgroundPositionX; CGFloat backgroundPositionY = self.backgroundPositionY; HippyBackgroundImageCacheManager *weakBackgroundCacheManager = [self backgroundCachemanager]; diff --git a/ios/sdk/debug/devtools/inspector/model/HippyPageModel.m b/ios/sdk/debug/devtools/inspector/model/HippyPageModel.m index 2df33e01e7e..352671aaa78 100644 --- a/ios/sdk/debug/devtools/inspector/model/HippyPageModel.m +++ b/ios/sdk/debug/devtools/inspector/model/HippyPageModel.m @@ -110,6 +110,7 @@ - (BOOL)screencastJSONWithManager:(HippyUIManager *)manager if (!rootView) { HippyLogWarn(@"PageModel, screen cast, root view is nil"); completion(@{}); + return; } CGFloat viewWidth = rootView.frame.size.width; CGFloat viewHeight = rootView.frame.size.height; @@ -118,6 +119,10 @@ - (BOOL)screencastJSONWithManager:(HippyUIManager *)manager CGFloat scaleX = self.maxSize.width / viewWidth; CGFloat scaleY = self.maxSize.height / viewHeight; scale = MIN(scaleX, scaleY); + } else { + HippyLogWarn(@"PageModel, screen cast, root view size is 0"); + completion(@{}); + return; } // root view snapshot UIGraphicsBeginImageContextWithOptions(rootView.frame.size, NO, scale); @@ -141,8 +146,7 @@ - (BOOL)screencastJSONWithManager:(HippyUIManager *)manager resultJSON[HippyPageKeySessionId] = @(timestamp); self.lastTimestamp = timestamp; completion(resultJSON); - } - else { + } else { completion(@{}); } });