On 18 Aug 2013, at 16:27, Simone Tellini <cocoa-...@tellini.info> wrote:

> 
> Il giorno 18/ago/2013, alle ore 15:03, "Gerriet M. Denkmann" 
> <gerr...@mdenkmann.de> ha scritto:
> 
>> I just noticed that the program I use to create Png files creates files with 
>> twice the number of pixels in each dimension.
>> Obviously because since I last used it, I have switched to a Retina Mac Book.
>> 
>> Ok, so I have to fix this program.
>> 
> 
> you can do something like this:
> 
>       NSSize                          size = ...;
>       NSBitmapImageRep        *bitmapRep = [[NSBitmapImageRep alloc] 
> initWithBitmapDataPlanes: NULL
>                                                                               
>                                                                          
> pixelsWide: size.width
>                                                                               
>                                                                          
> pixelsHigh: size.height
>                                                                               
>                                                                   
> bitsPerSample: 8
>                                                                               
>                                                                 
> samplesPerPixel: 4
>                                                                               
>                                                                            
> hasAlpha: YES
>                                                                               
>                                                                            
> isPlanar: NO
>                                                                               
>                                                                  
> colorSpaceName: NSCalibratedRGBColorSpace
>                                                                               
>                                                                    
> bitmapFormat: NSAlphaFirstBitmapFormat
>                                                                               
>                                                                         
> bytesPerRow: 0
>                                                                               
>                                                                    
> bitsPerPixel: 0];
>       NSGraphicsContext       *bmCtx;
>       NSAffineTransform       *transform = [NSAffineTransform transform]; 
> 
>       [bitmapRep setSize: size];
>       
>       bmCtx = [NSGraphicsContext graphicsContextWithBitmapImageRep: 
> bitmapRep];
>       bmCtx = [NSGraphicsContext graphicsContextWithGraphicsPort: [bmCtx 
> graphicsPort]
>                                                                               
>                            flipped: YES];
> 
>       [NSGraphicsContext saveGraphicsState];
>       [NSGraphicsContext setCurrentContext: bmCtx];
> 
>       [transform translateXBy: 0
>                                               yBy: size.height];
>       [transform scaleXBy: 1
>                                       yBy: -1];
>       [transform set];
> 
> ...do some drawing...
> 
>       [NSGraphicsContext restoreGraphicsState];
> 
>       data = [bitmapRep representationUsingType: NSPNGFileType properties: 
> nil];
>       
>       [bitmapRep release];

No need for the complex cocoa to CG manoeuvrings there, this will do fine:
NSSize size = …;
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(NULL, size.width, size.height, 8, 8 * 
4 * size.width, rgbColorSpace, kCGBitmapAlphaInfoMask & kCGAlphaLast);
CFRelease(rgbColorSpace);
… do some drawing …
CGImageRef img = CGBitmapContextCreateImage(ctx);
CGContextRelease(ctx);
return [[[NSImage alloc] initWithCGImage:img size:size] autorelease];

Thanks

Tom Davie
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to