Re: NSImage from bitmap - then delete bitmap
Re: NSImage from bitmap - then delete bitmap
- Subject: Re: NSImage from bitmap - then delete bitmap
- From: Graham Cox <email@hidden>
- Date: Fri, 22 Jul 2016 16:29:58 +1000
> On 22 Jul 2016, at 4:08 PM, Trygve Inda <email@hidden> wrote:
>
> I don't think the second part will work because of my workflow:
>
> At Launch: Create pixel buffer that is 1000 x 1000 pixels
>
> Looping thread
> 1. Fill pixel buffer with pixels based on some algorithm
> 2. create an NSImage with these pixels
> 3. pass it to the main thread to be drawn in a window
> Restart the loop with a slightly modified algorithm
>
> If calling this:
> [myImage addRepresentation:theBitmapIMadeFirst];
>
> Only causes the NSImage to retain it, then when I change the pixels in the
> bitmap, they get changed in the NSImage.
>
> I need the NSImage to keep the pixels as they existed when I created it.
Do you?
Once the image is drawn into the window, it’s effectively copied to the window’s backing store, so there’s already a second copy of the pixels.
The only situation that might cause a problem is if the window can possibly be called upon to redraw at a time when the bitmap is being updated but hasn’t yet completed. If the code that fills the buffer is synchronous, that can never happen. If the code is asynchronous (e.g. on a second thread), then you
*might* want a copy of the bitmap, but only if drawing it halfway through updating would actually be bad - that depends entirely on what is being drawn. If it’s a minor/subtle change, it may not be noticeable.
With half an eye on performance, if you *do* strictly need a copy of the bitmap, note that NSBitmapImageRep conforms to NSCopying. You don’t have to turn it into a TIFF and back again.
Also, you don’t even need an NSImage - the NSImageRep can be drawn directly.
—Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden