Merge pull request #3337 from GetToSet/ethanwong/nsscreen-thread

Fix a crash of accessing -[NSScreen backingScaleFactor] from background thread.
This commit is contained in:
DreamPiggy 2022-04-02 17:48:17 +08:00 committed by GitHub
commit d01eb3f8ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 6 deletions

View File

@ -132,7 +132,13 @@
#elif SD_UIKIT #elif SD_UIKIT
CGFloat screenScale = [UIScreen mainScreen].scale; CGFloat screenScale = [UIScreen mainScreen].scale;
#elif SD_MAC #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 #endif
self.scale = screenScale; self.scale = screenScale;
self.opaque = NO; self.opaque = NO;
@ -166,7 +172,13 @@
#elif SD_UIKIT #elif SD_UIKIT
CGFloat screenScale = [UIScreen mainScreen].scale; CGFloat screenScale = [UIScreen mainScreen].scale;
#elif SD_MAC #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 #endif
self.scale = screenScale; self.scale = screenScale;
self.opaque = NO; self.opaque = NO;

View File

@ -17,7 +17,13 @@ static void *kNSGraphicsContextScaleFactorKey;
static CGContextRef SDCGContextCreateBitmapContext(CGSize size, BOOL opaque, CGFloat scale) { static CGContextRef SDCGContextCreateBitmapContext(CGSize size, BOOL opaque, CGFloat scale) {
if (scale == 0) { if (scale == 0) {
// Match `UIGraphicsBeginImageContextWithOptions`, reset to the scale factor of the devices main screen if scale is 0. // Match `UIGraphicsBeginImageContextWithOptions`, reset to the scale factor of the devices 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 width = ceil(size.width * scale);
size_t height = ceil(size.height * scale); size_t height = ceil(size.height * scale);
@ -106,7 +112,13 @@ UIImage * SDGraphicsGetImageFromCurrentImageContext(void) {
} }
if (!scale) { if (!scale) {
// reset to the scale factor of the devices main screen if scale is 0. // reset to the scale factor of the devices 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]; NSImage *image = [[NSImage alloc] initWithCGImage:imageRef scale:scale orientation:kCGImagePropertyOrientationUp];
CGImageRelease(imageRef); CGImageRelease(imageRef);

View File

@ -28,7 +28,13 @@ inline CGFloat SDImageScaleFactorForKey(NSString * _Nullable key) {
#elif SD_UIKIT #elif SD_UIKIT
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
#elif SD_MAC #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 #endif
{ {
// a@2x.png -> 8 // a@2x.png -> 8

View File

@ -18,7 +18,13 @@ static NSArray *SDBundlePreferredScales() {
#elif SD_UIKIT #elif SD_UIKIT
CGFloat screenScale = [UIScreen mainScreen].scale; CGFloat screenScale = [UIScreen mainScreen].scale;
#elif SD_MAC #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 #endif
if (screenScale <= 1) { if (screenScale <= 1) {
scales = @[@1,@2,@3]; scales = @[@1,@2,@3];