Re: Saving PNG files from NSImage
Re: Saving PNG files from NSImage
- Subject: Re: Saving PNG files from NSImage
- From: Mike Abdullah <email@hidden>
- Date: Mon, 29 Aug 2011 16:19:20 +0100
This code is making the assumption that [reps objectAtIndex:0] is a bitmap image rep. NSImage makes no such guarantees. Points:
- Walk through all the image reps, looking for the best bitmap
- If no bitmap exists, you'll have to create one by drawing the image into a bitmap context
- If you're writing this fallback code, might as well make it your main code, and always draw into a bitmap context, rather than an NSImage rep
On 29 Aug 2011, at 14:11, Martin Linklater wrote:
> Thanks Marco. When I call representationUsingType I get this:
>
> -[NSCGImageSnapshotRep representationUsingType:properties:]: unrecognized selector sent to instance 0x100220610
> 2011-08-29 14:08:16.161 TexLifter[69386:407] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCGImageSnapshotRep representationUsingType:properties:]: unrecognized selector sent to instance 0x100220610'
>
> Here is my full code:
>
> NSSize outputImageSize;
> outputImageSize.width = dimension;
> outputImageSize.height = dimension;
> self.outputImage = [[NSImage alloc] initWithSize:outputImageSize];
>
> // Do a load of -[image drawAtPoint:fromRect:operation:fraction] calls to composite
>
> NSArray* reps = self.outputImage.representations;
>
> NSBitmapImageRep* rep = [reps objectAtIndex:0];
>
> NSData* data = [rep representationUsingType:NSPNGFileType properties:nil];
>
> *ERROR AS ABOVE*
>
> 8(
>
> On 29 Aug 2011, at 13:59, Marco Tabini wrote:
>
>> Hi Martin—
>>
>> On 2011-08-29, at 8:18 AM, Martin Linklater wrote:
>>
>>> Can anyone point me in the right direction as to how to save my NSImage to a PNG file ? Or some documentation which actually describes how to convert the NSCGImageSnapshotRep into something I can save out ? I've spent a few hours on this now and I can't believe there isn't an easy solution I'm missing.
>>
>>
>> Take a look here: http://stackoverflow.com/questions/4438802/nscgimagesnapshotrep-how-get-bitmapdata
>>
>> HTH,
>>
>>
>> —Mt.
>>
>> --
>> Phone: +1-416-630-6202 x.666
>> Twitter: @mtabini
>>
>>
>
> _______________________________________________
>
> 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
_______________________________________________
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