Hi Ulrich,
thanks for the “release” explanation.
Before writing my last post, I had already tried to make a function returning a pict from NSImage (in my case ptr). In fact I had copy/pasted in a new method the piece of code (originally form macOSLib) needed to return a systemImage (below). The debugger run without giving warnings, yet a strange thing happened: the target canvas would not get the pict; instead, another canvas that set to show a systemIcon would return blank. I quitted Xojo and re-opened it several times, but to no avail.
That’s why I asked you if there were a simple way to return a pct from ptr.
Now the funny thing: yesterday, as I do every week, I restarted my MBP, and voila, the target canvas was showing the intended pict, and the systemIcon-canvas had its right pict. May be if I had cleared Xojo cache the issue would have disappeared.
Then this morning I got your kind reply with your proposed code, and I see that you use a more direct approch. After a quick test in the debugger I started making some necessary modifications; first of all in the declaration where after replacing NSImage with Ptr, I added also width and Heigh, since a ptr does not have them.
Then, for the same reason I removed i.valid; and after that I fall in the mud…
That makes me uncomfortable. Because asking for help is legit, but pestering for help shows lack of decency.
Therefore, if you agree, and although I like your approach, I’d consider this issue satisfactorily solved.
And I thank you again for your assistance.
Chapter 2: [quote]And I would be interested in why you left MacOSLib too, if you wouldnt mind telling me.[/quote]
Answer coming.
Public Function NSImageToPicture(extends iPtr as ptr, iW as Integer, iH as Integer) as picture
declare function size lib “Cocoa” selector “size” (obj_id as Ptr) as NSSize
//get the size of the NSImage
dim s As NSSize
s.width = iW
s.height = iH
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(Graphics.HandleTypeCGContextRef))
dim rect As NSRect
rect.x = 0
rect.y = 0
rect.w = iW
rect.h = iH
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
End Function