From 30c95635112836059f4b76d287913484631563b8 Mon Sep 17 00:00:00 2001 From: Ethan Wong Date: Mon, 28 Mar 2022 20:17:53 +0800 Subject: [PATCH] Fix crash of accessing mainScreen from background thread on 10.11. --- SDWebImage/Core/SDGraphicsImageRenderer.m | 16 ++++++++++++++-- SDWebImage/Core/SDImageGraphics.m | 16 ++++++++++++++-- SDWebImage/Core/SDWebImageDefine.m | 8 +++++++- SDWebImage/Private/SDImageAssetManager.m | 8 +++++++- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/SDWebImage/Core/SDGraphicsImageRenderer.m b/SDWebImage/Core/SDGraphicsImageRenderer.m index 03aef3a5..aeda97b1 100644 --- a/SDWebImage/Core/SDGraphicsImageRenderer.m +++ b/SDWebImage/Core/SDGraphicsImageRenderer.m @@ -132,7 +132,13 @@ #elif SD_UIKIT CGFloat screenScale = [UIScreen mainScreen].scale; #elif SD_MAC - CGFloat screenScale = [NSScreen mainScreen].backingScaleFactor; + NSScreen *mainScreen = nil; + if (@available(macOS 10.12, *)) { + mainScreen = [NSScreen mainScreen]; + } else { + mainScreen = [NSScreen screens].firstObject; + } + CGFloat screenScale = mainScreen.backingScaleFactor ?: 1.0f; #endif self.scale = screenScale; self.opaque = NO; @@ -166,7 +172,13 @@ #elif SD_UIKIT CGFloat screenScale = [UIScreen mainScreen].scale; #elif SD_MAC - CGFloat screenScale = [NSScreen mainScreen].backingScaleFactor; + NSScreen *mainScreen = nil; + if (@available(macOS 10.12, *)) { + mainScreen = [NSScreen mainScreen]; + } else { + mainScreen = [NSScreen screens].firstObject; + } + CGFloat screenScale = mainScreen.backingScaleFactor ?: 1.0f; #endif self.scale = screenScale; self.opaque = NO; diff --git a/SDWebImage/Core/SDImageGraphics.m b/SDWebImage/Core/SDImageGraphics.m index f6f89279..d1a1ca65 100644 --- a/SDWebImage/Core/SDImageGraphics.m +++ b/SDWebImage/Core/SDImageGraphics.m @@ -17,7 +17,13 @@ static void *kNSGraphicsContextScaleFactorKey; static CGContextRef SDCGContextCreateBitmapContext(CGSize size, BOOL opaque, CGFloat scale) { if (scale == 0) { // Match `UIGraphicsBeginImageContextWithOptions`, reset to the scale factor of the device’s main screen if scale is 0. - scale = [NSScreen mainScreen].backingScaleFactor; + NSScreen *mainScreen = nil; + if (@available(macOS 10.12, *)) { + mainScreen = [NSScreen mainScreen]; + } else { + mainScreen = [NSScreen screens].firstObject; + } + scale = mainScreen.backingScaleFactor ?: 1.0f; } size_t width = ceil(size.width * scale); size_t height = ceil(size.height * scale); @@ -106,7 +112,13 @@ UIImage * SDGraphicsGetImageFromCurrentImageContext(void) { } if (!scale) { // reset to the scale factor of the device’s main screen if scale is 0. - scale = [NSScreen mainScreen].backingScaleFactor; + NSScreen *mainScreen = nil; + if (@available(macOS 10.12, *)) { + mainScreen = [NSScreen mainScreen]; + } else { + mainScreen = [NSScreen screens].firstObject; + } + scale = mainScreen.backingScaleFactor ?: 1.0f; } NSImage *image = [[NSImage alloc] initWithCGImage:imageRef scale:scale orientation:kCGImagePropertyOrientationUp]; CGImageRelease(imageRef); diff --git a/SDWebImage/Core/SDWebImageDefine.m b/SDWebImage/Core/SDWebImageDefine.m index 845730f0..aee8ca4d 100644 --- a/SDWebImage/Core/SDWebImageDefine.m +++ b/SDWebImage/Core/SDWebImageDefine.m @@ -28,7 +28,13 @@ inline CGFloat SDImageScaleFactorForKey(NSString * _Nullable key) { #elif SD_UIKIT if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) #elif SD_MAC - if ([[NSScreen mainScreen] respondsToSelector:@selector(backingScaleFactor)]) + NSScreen *mainScreen = nil; + if (@available(macOS 10.12, *)) { + mainScreen = [NSScreen mainScreen]; + } else { + mainScreen = [NSScreen screens].firstObject; + } + if ([mainScreen respondsToSelector:@selector(backingScaleFactor)]) #endif { // a@2x.png -> 8 diff --git a/SDWebImage/Private/SDImageAssetManager.m b/SDWebImage/Private/SDImageAssetManager.m index 50a4da94..7bca347c 100644 --- a/SDWebImage/Private/SDImageAssetManager.m +++ b/SDWebImage/Private/SDImageAssetManager.m @@ -18,7 +18,13 @@ static NSArray *SDBundlePreferredScales() { #elif SD_UIKIT CGFloat screenScale = [UIScreen mainScreen].scale; #elif SD_MAC - CGFloat screenScale = [NSScreen mainScreen].backingScaleFactor; + NSScreen *mainScreen = nil; + if (@available(macOS 10.12, *)) { + mainScreen = [NSScreen mainScreen]; + } else { + mainScreen = [NSScreen screens].firstObject; + } + CGFloat screenScale = mainScreen.backingScaleFactor ?: 1.0f; #endif if (screenScale <= 1) { scales = @[@1,@2,@3];