ChartsOrg / ChartsOrg/Charts

NSBitmapImageRep deprecated from macOS 10.14: SOLUTION!

Open
#5,106 1 comment 0 reactions 1 assignee Claimed by @pmairoldi View on GitHub
Dominant language
Swift
Stars
28k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

## What did you do?

just compiled Mac OS Example for Mac OS 14.0 with Xcode 15 (15A240d)

## What did you expect to happen?

no warnings when compiling for MacOS 10.14 and newer

## What happened instead?

warning: 'init(focusedViewRect:)' was deprecated in macOS 10.14: Use -[NSView cacheDisplayInRect:toBitmapImageRep:] to snapshot a view.
in Platform+Graphics - functions: NSUIImagePNGRepresentation, NSUIImageJPEGRepresentation!

## DGCharts Environment

DGCharts version: latest master
Xcode version: Xcode 15 (15A240d)
Swift version: 5
Platform(s) running DGCharts: MacOS 17.01
macOS version running Xcode: 17.01

## Possible fix:

**BEGIN OF INSTRUCTIONS**

**_CHANGE IN FILE "ChartViewBase.swift" :_**

open func save(to path: String, format: ImageFormat, compressionQuality: Double) -> Bool
{
guard let image = getChartImage(transparent: format != .jpeg) else { return false }

let imageData: Data?
switch (format)
{
case .png: imageData = NSUIImagePNGRepresentation(image, inView: self)
case .jpeg: imageData = NSUIImageJPEGRepresentation(image, CGFloat(compressionQuality), inView: self)
}

guard let data = imageData else { return false }

do
{
try data.write(to: URL(fileURLWithPath: path), options: .atomic)
}
catch
{
return false
}

return true
}

**_CHANGE in FILE "Platform+Graphics.swift"_**

func NSUIImagePNGRepresentation(_ image: NSUIImage, inView view: NSUIView) -> Data?
{
image.lockFocus()

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 tRep?.representation(using: .png, properties: [:])
}

func NSUIImageJPEGRepresentation(_ image: NSUIImage, _ quality: CGFloat = 0.9, inView view: NSUIView) -> Data?
{
image.lockFocus()

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 tRep?.representation(using: .jpeg, properties: [NSBitmapImageRep.PropertyKey.compressionFactor: quality])
}

**END OF INSTRUCTIONS**

## Comments:

Sorry for this unusual reply, but I have no access to add a new branch to your Rep-
Hope it helps anyway.

Any Reply/Questions can be send to: wbernard@me.com

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.