Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSBitmapImageRep deprecated from macOS 10.14: SOLUTION! #5106

Open
McWare opened this issue Sep 24, 2023 · 1 comment
Open

NSBitmapImageRep deprecated from macOS 10.14: SOLUTION! #5106

McWare opened this issue Sep 24, 2023 · 1 comment
Assignees

Comments

@McWare
Copy link

McWare commented Sep 24, 2023

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

himanshulahoti20 added a commit to himanshulahoti20/Charts that referenced this issue Jan 8, 2024
Fixed the NSBitmapImageRep deprecated from macOS 10.14.
@pmairoldi
Copy link
Collaborator

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.

@pmairoldi pmairoldi self-assigned this Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants