Merge pull request #3337 from GetToSet/ethanwong/nsscreen-thread
Fix a crash of accessing -[NSScreen backingScaleFactor] from background thread.
This commit is contained in:
commit
d01eb3f8ac
|
@ -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;
|
||||||
|
|
|
@ -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 device’s main screen if scale is 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 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 device’s main screen if scale is 0.
|
// 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];
|
NSImage *image = [[NSImage alloc] initWithCGImage:imageRef scale:scale orientation:kCGImagePropertyOrientationUp];
|
||||||
CGImageRelease(imageRef);
|
CGImageRelease(imageRef);
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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];
|
||||||
|
|
Loading…
Reference in New Issue