Re: NSImage from bitmap - then delete bitmap
Re: NSImage from bitmap - then delete bitmap
- Subject: Re: NSImage from bitmap - then delete bitmap
- From: Ken Thomases <email@hidden>
- Date: Fri, 22 Jul 2016 01:03:48 -0500
On Jul 22, 2016, at 12:37 AM, Trygve Inda <email@hidden> wrote:
>
> I create an NSBitmapImageRep:
>
> [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
> pixelsWide:pixelSize.width
> pixelsHigh:pixelSize.height
> bitsPerSample:8
> samplesPerPixel:4
> hasAlpha:YES
> isPlanar:NO
> colorSpaceName:NSDeviceRGBColorSpace
> bitmapFormat:NSAlphaFirstBitmapFormat
> bytesPerRow:pixelSize.width * 4
> bitsPerPixel:32]
>
> I then get the address by sending a "bitmapData" message to the object
>
> After filling it with image data, I call:
>
> NSImage* image =
> [[NSImage alloc] initWithData:[myImageRep TIFFRepresentation]];
Aside from your primary question, don't do this. Use -initWithSize: and then directly add the representation to the image using -addRepresentation:.
> So now I have an NSImage. What happens if I delete/release myImageRep (my
> NSBitmapImageRep)?
>
> Has the call to NSImage copied my pixels so that they are self-contained
> within the NSImage?
The general answer is you don't know and you shouldn't care. It is the responsibility of the framework to do what's necessary. It may have retained the image rep or retained some private NSData object that represented the pixels or copied the data or whatever.
The memory management conventions of Cocoa are designed to be local in nature. Generally, you do not need to know what other code is doing. (Exceptions are documented.) You only need to get your part correct, where you retain objects if you need to ensure they live beyond the current scope (if you don't already have an ownership reference to them, as from +alloc, -copy…, etc.) and you release ownership references after you're done with those objects. You just assume that the other parts of the code follow the rules, too, and everything works.
You said "delete/release myImageRep". That "delete" represents incorrect thinking on your part. You only ever release your ownership reference to the object. You can't know whether something else also has an ownership reference to it, so you can't know when the object is deleted/deallocated.
In this particular case, you're using -TIFFRepresentation which creates a data object of a TIFF image file format. Since that's not the same format as the pixel data you stored via -bitmapData, it's very likely an independent object. Also, there's no reason for the image rep to keep any reference to it around. For example, you could modify the bitmap data some more and generate a new TIFF data object and that would be different from the first. Surely, it's clear to you that such modifications to the bitmap data of the image rep must not modify the TIFF data that you created earlier.
Then, -initWithData: decodes the TIFF data and makes a new NSImage with whatever representations it needs. Because of the layers of abstraction/conversion that the data went through, it's very unlikely that the new NSImage or representations have any references to the original image rep.
Regards,
Ken
_______________________________________________
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