ReactNative中升级IOS 17版本Crash办理
ReactNative中升级IOS 17版本Crash办理https://img-blog.csdnimg.cn/c41734c10e904598aee5b3f86901fbc4.png#pic_center
一、标题形貌
业务上用了截图相干UIGraphicsBeginImageContextWithOptions && UIGraphicsEndImageContext 会报出 Assert。
错误信息会是下面如许:
[*]UIGraphicsBeginImageContext() failed to allocate CGBitampContext: size={382, 0}, scale=3.000000, bitmapInfo=0x2002. Use UIGraphicsImageRenderer to avoid this assert.
或者会是如许的
[*]*** Assertion failure in void _UIGraphicsBeginImageContextWithOptions(CGSize, BOOL, CGFloat, BOOL)(), UIGraphics.m:410
二、原因分析
查了下 api,发现 UIGraphicsBeginImageContext 在iOS 17上已经 deprecated 了. 点击这里,看看官方文档 官方文档说明https://developer.apple.com/documentation/uikit/1623922-uigraphicsbeginimagecontext
https://img-blog.csdnimg.cn/e0a2851fd9c94f9ca4ad695c51fb76e3.png
可以或许清楚地看到针对以下版本废弃
[*]iOS 2.0–17.0 Deprecated
[*]iPadOS 2.0–17.0 Deprecated
[*]Mac Catalyst 13.1–17.0 Deprecated
[*]tvOS 9.0–17.0 Deprecated
[*]watchOS 2.0–10.0 Deprecated
[*]visionOS 1.0–1.0 Deprecated
三、办理方案决策
3.1 设置宽高为非零值
当我们包管api 宽高不为零,则可正常使用,必要改动的地方较多,大概必要将业务中每一个设置为零属性的地方都得检查
3.2 使用新的UIGraphicsImageRenderer替换就版本的UIGraphicsBeginImageContext
改动较小,只必要改动三方库中原生内容即可,无需针对业务中涉及到的每一个元素举行校验,只必要验证部门页面展示正常即可。
四、大概使用到该API的三方库
4.1 react-native-fast-image
办理方案:react-native-fast-image 官方办理
修改node_modules文件路径: ios/FastImage/FFFastImageView.m
- (UIImage*) makeImage: (UIImage*)image withTint: (UIColor*)color {
UIImage* newImage = ;
UIGraphicsImageRendererFormat *format = ;
UIGraphicsImageRenderer *renderer = [ initWithSize:image.size format:format];
UIImage *resultImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
;
;
}];
return resultImage;
}
4.2 react-native-linear-gradient
官方办理方案: react-native-linear-gradient 官方办理
按照官方办理,已经提供了新的版本包在npm仓库,此时我们可以去更新源代码或者直接更新版本:
[*] 下载是最新的版本
下载最新版本地点:https://github.com/react-native-linear-gradient/react-native-linear-gradient/releases/tag/v2.8.3
也可以直接去看看npm仓库。
[*] 直接更新源码
如果要更新源代码,则举行更新路径/node_modules/react-native-linear-gradient/ios/BVLinearGradientLayer.m 文件中 display函数。
- (void)display {
;
// short circuit when height or width are 0. Fixes CGContext errors throwing
if (self.bounds.size.height == 0 || self.bounds.size.width == 0) {
return;
}
BOOL hasAlpha = NO;
for (NSInteger i = 0; i < self.colors.count; i++) {
hasAlpha = hasAlpha || CGColorGetAlpha(self.colors.CGColor) < 1.0;
}
if (@available(iOS 10.0, *)) {
UIGraphicsImageRendererFormat *format;
if (@available(iOS 11.0, *)) {
format = ;
} else {
format = ;
}
format.opaque = !hasAlpha;
UIGraphicsImageRenderer *renderer = [ initWithSize:self.bounds.size format:format];
UIImage *image = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull ref) {
;
}];
self.contents = (__bridge id _Nullable)(image.CGImage);
self.contentsScale = image.scale;
} else {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, !hasAlpha, 0.0);
CGContextRef ref = UIGraphicsGetCurrentContext();
;
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
self.contents = (__bridge id _Nullable)(image.CGImage);
self.contentsScale = image.scale;
UIGraphicsEndImageContext();
}
}
我项目中闪退的就是这个库出的标题,当渐变的文本有内容时,则展示是正常的,但是当文本内容不存在时,就会出现闪退,这就很诡异。
4.3 使用到 UIGraphicsBeginImageContextWithOptions 的三方库还有以下一些:
[*]react native自己绘画边框
[*]react-native自己图片处理 /node_modules/react-native/Libraries/Image/RCTImageUtils.m
[*]react-native-camera
[*]react-native-view-shot
[*]react-native-svg 文件包含地点: node_modules/react-native-svg/apple/RNSVGRenderable.mm
[*]react-native-syan-image-picker 中 TZImagePickerController
其他三方库没有列出来,可以在Xcode中举行搜索 UIGraphicsBeginImageContextWithOptions ,检查是否是有使用到,同时验证表现正常与否。如果表现有标题,则可以去三方库对应的官方Github寻求办理方案或者自行替换。
五、参考地点
Apple官方提问:https://developer.apple.com/forums/thread/733326
Apple官方提问:https://developer.apple.com/forums/thread/731385
github issue:https://github.com/react-native-linear-gradient/react-native-linear-gradient/issues/637
github: https://github.com/ibireme/YYText/issues/984
github:https://github.com/muntius/react-native-fast-image/commit/fc2b8acd97f07989e312f5cbd61d2e541fda3611
github:https://github.com/DylanVann/react-native-fast-image/issues/1002
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]