From eb48c218cd574a101e3ed7bbc14cd954847e7b3b Mon Sep 17 00:00:00 2001 From: Himanshu Lahoti Date: Mon, 8 Jan 2024 14:12:08 +0530 Subject: [PATCH] #5106 Fixed the NSBitmapImageRep deprecated from macOS 10.14. --- .../ChartsDemo-iOS.xcodeproj/project.pbxproj | 11 ++++++--- Source/Charts/Utils/Platform+Graphics.swift | 23 ++++++++++++++----- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/ChartsDemo-iOS/ChartsDemo-iOS.xcodeproj/project.pbxproj b/ChartsDemo-iOS/ChartsDemo-iOS.xcodeproj/project.pbxproj index 69441f58f5..531694b22c 100644 --- a/ChartsDemo-iOS/ChartsDemo-iOS.xcodeproj/project.pbxproj +++ b/ChartsDemo-iOS/ChartsDemo-iOS.xcodeproj/project.pbxproj @@ -651,11 +651,12 @@ 225B361C1F6EB9A50005B3D5 = { CreatedOnToolsVersion = 9.0; LastSwiftMigration = 1150; + ProvisioningStyle = Manual; }; 5B57BBAE1A9B26AA0036A6CC = { CreatedOnToolsVersion = 6.1.1; LastSwiftMigration = 1150; - ProvisioningStyle = Automatic; + ProvisioningStyle = Manual; }; }; }; @@ -890,6 +891,7 @@ CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CODE_SIGN_STYLE = Manual; DEBUG_INFORMATION_FORMAT = dwarf; DEVELOPMENT_TEAM = ""; GCC_C_LANGUAGE_STANDARD = gnu11; @@ -898,6 +900,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.dcg.ChartsDemo-Swift"; PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_VERSION = 5.0; @@ -921,6 +924,7 @@ CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CODE_SIGN_STYLE = Manual; COPY_PHASE_STRIP = NO; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEVELOPMENT_TEAM = ""; @@ -930,6 +934,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "com.dcg.ChartsDemo-Swift"; PRODUCT_NAME = "$(TARGET_NAME)"; + PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = 1; }; @@ -1046,7 +1051,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CODE_SIGN_IDENTITY = "Apple Development"; - CODE_SIGN_STYLE = Automatic; + CODE_SIGN_STYLE = Manual; DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = "Supporting Files/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 13.0; @@ -1069,7 +1074,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CODE_SIGN_IDENTITY = "Apple Development"; - CODE_SIGN_STYLE = Automatic; + CODE_SIGN_STYLE = Manual; DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = "Supporting Files/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 13.0; diff --git a/Source/Charts/Utils/Platform+Graphics.swift b/Source/Charts/Utils/Platform+Graphics.swift index a8427a4fb6..56c993f880 100644 --- a/Source/Charts/Utils/Platform+Graphics.swift +++ b/Source/Charts/Utils/Platform+Graphics.swift @@ -90,20 +90,31 @@ func NSUIGraphicsPopContext() NSGraphicsContext.restoreGraphicsState() } -func NSUIImagePNGRepresentation(_ image: NSUIImage) -> Data? +func NSUIImagePNGRepresentation(_ image: NSUIImage, inView view: NSUIView) -> Data? { image.lockFocus() - let rep = NSBitmapImageRep(focusedViewRect: NSMakeRect(0, 0, image.size.width, image.size.height)) + + let tRep = view.bitmapImageRepForCachingDisplay(in: NSRect(x: 0, y: 0, width: image.size.width, height: image.size.height)) + if let rep = tRep { + view.cacheDisplay(in: NSRect(x: 0, y: 0, width: image.size.width, height: image.size.height), to: rep) + return rep.representation(using: .png, properties: [:]) + } image.unlockFocus() - return rep?.representation(using: .png, properties: [:]) + return tRep?.representation(using: .png, properties: [:]) } -func NSUIImageJPEGRepresentation(_ image: NSUIImage, _ quality: CGFloat = 0.9) -> Data? +func NSUIImageJPEGRepresentation(_ image: NSUIImage, _ quality: CGFloat = 0.9, inView view: NSUIView) -> Data? { image.lockFocus() - let rep = NSBitmapImageRep(focusedViewRect: NSMakeRect(0, 0, image.size.width, image.size.height)) + + let tRep = view.bitmapImageRepForCachingDisplay(in: NSRect(x: 0, y: 0, width: image.size.width, height: image.size.height)) + if let rep = tRep { + view.cacheDisplay(in: NSRect(x: 0, y: 0, width: image.size.width, height: image.size.height), to: rep) + return rep.representation(using: .jpeg, properties: [NSBitmapImageRep.PropertyKey.compressionFactor: quality]) + } + image.unlockFocus() - return rep?.representation(using: .jpeg, properties: [NSBitmapImageRep.PropertyKey.compressionFactor: quality]) + return tRep?.representation(using: .jpeg, properties: [NSBitmapImageRep.PropertyKey.compressionFactor: quality]) } private var imageContextStack: [CGFloat] = []