I’m trying to get the sort arrow using NSImageNamed constants.
I’ve adapted the code from this post and it works well on non-retina systems.
When I try to request the image at double size and shrink it down in the canvas, the final output still looks poor. But from what I’m trying to understand it seems this is how you’re supposed to do it. Did I miss something?
I know what you’re going to say,
“Yada yada yada, 2016r1.”
But this needs to be backwards compatible and I’m losing my fsck()
ing mind.
I don’t have a retina monitor, so this is getting on my nerves really fast.
I can’t debug with 55 characters across of space (I have to use the half resolution method)
Request code:
#if TargetMacOS then
Declare Function NSClassFromString Lib "Cocoa" (aClassName As CFStringRef) As Ptr
Declare Function imageNamed Lib "Cocoa" selector "imageNamed:" _
(class_id As Ptr, name As CFStringRef) As Ptr
//get the NSImage
Dim iPtr As Ptr = imageNamed(NSClassFromString("NSImage"), "NSAscendingSortIndicator")
//we have an NSImage
if iPtr <> Nil Then
declare function size lib "Cocoa" selector "size" (obj_id as Ptr) as NSSize
//get the size of the NSImage
dim s As NSSize //= size(iPtr)
s.width = w
s.height = h
dim p as new picture(s.width,s.height)
//get the graphics of the picture as CGContext
dim cntx as Ptr=ptr(p.Graphics.Handle(5)) // Graphics.HandleTypeCGContextRef
dim rect As NSRect
rect.rsize=s
declare function CGImageForProposedRect lib "Cocoa" _
selector "CGImageForProposedRect:context:hints:" _
(obj_id as Ptr, byref proposedDestRect as NSRect, _
referenceContext as Ptr, hints as Ptr) as Ptr
//convert the NSImage to CGImage
dim cgPtr As ptr=CGImageForProposedRect(iPtr,rect,nil,nil)
soft declare sub CGContextDrawImage lib "Cocoa" _
(context as Ptr, rect as NSRect, image as Ptr)
//Draw the CGImage to our Xojo picture
CGContextDrawImage cntx, rect, cgPtr
Return p
else
// An error occurred
end
#endif
Draw Code:
g.DrawPicture(pIndicatorPicture, PositionX, PositionY, _
iIndicatorW, iIndicatorH, 0, 0, iIndicatorW * ScalingFactor, iIndicatorH * ScalingFactor)
PositionX and PositionY are defined and work well.
iIndicatorW and iIndicatorH are both 12. The multiplied versions are what goes into the request function.
The image I get back from NSImage is REALLY pixelated when it comes out at 24x24, and is still pixelated when shrunk to 12 by 12.
Send help, and liquor.