ChartsOrg/Charts

NSBitmapImageRep deprecated from macOS 10.14: SOLUTION!

McWare opened this issue · 1 comments

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

You do not need access to our repo. Make the change in your fork of this project and create a pull request to this project.