Fix that the graphics helper method will return nil when scale = 0, match the UIKit behavior

This commit is contained in:
DreamPiggy 2018-10-31 14:40:10 +08:00
parent f5cc328684
commit ee6f99f204
1 changed files with 4 additions and 4 deletions

View File

@ -18,6 +18,10 @@
static void *kNSGraphicsContextScaleFactorKey;
static CGContextRef SDCGContextCreateBitmapContext(CGSize size, BOOL opaque, CGFloat scale) {
if (scale == 0) {
// Match `UIGraphicsBeginImageContextWithOptions`, reset to the scale factor of the devices main screen if scale is 0.
scale = [NSScreen mainScreen].backingScaleFactor;
}
size_t width = ceil(size.width * scale);
size_t height = ceil(size.height * scale);
if (width < 1 || height < 1) return NULL;
@ -30,10 +34,6 @@ static CGContextRef SDCGContextCreateBitmapContext(CGSize size, BOOL opaque, CGF
if (!context) {
return NULL;
}
if (scale == 0) {
// Match `UIGraphicsBeginImageContextWithOptions`, reset to the scale factor of the devices main screen if scale is 0.
scale = [NSScreen mainScreen].backingScaleFactor;
}
CGContextScaleCTM(context, scale, scale);
return context;