On 19 Aug 2013, at 14:17, Graham Cox <[email protected]> wrote:
>
> On 19/08/2013, at 9:01 AM, Gerriet M. Denkmann <[email protected]> wrote:
>
>> I seem to need a way to tell NSImage NOT to double the pixels for me, but I
>> don't see any way to do this.
>> Like: [ image setBackingScaleFactor: 1 ] but no such method seems to exist.
>>
>>
>> So hardcoding the retina-ness of my current computer seems to be the only
>> solution.
>
>
> Of course it isn't.
>
> Marcel's answer looks correct to me - forget NSImage, use NSBitmapImageRep
> instead (without adding it to an NSImage). Instead of -lockFocus, etc, just
> create a context using the bitmap rep and set it as the current context. Then
> you can draw using either high-level stuff that uses "the current context" or
> lower level CG... functions.
>
> The problem you're running into is that NSImage is trying to be too darn
> smart. Don't give it the chance, by not using it AT ALL.
Yes, this really the crux of the matter. Once I eliminated NSImage, everything
started working.
Here is the final code (if there is anything wrong or not quite right, please
feel free to critizise it):
#define USE_IMAGEx // Off. Works, but has Retina hard-coded.
#ifdef USE_IMAGE
finalWidth /= 2; // hardcoded Retina Fix
#endif
CGFloat scaleFactor = finalWidth / bounds.size.width;
NSSize sizE = NSMakeSize( finalWidth, bounds.size.height * scaleFactor );
#ifdef USE_IMAGE
NSImage *image = [[NSImage alloc] initWithSize: sizE];
[image setFlipped:YES];
[image lockFocus];
#else // USE_BITMAP
NSInteger width = (NSInteger)sizE.width;
NSInteger height = (NSInteger)sizE.height;
NSInteger bps = 8;
NSInteger spp = 4;// RGBA
NSBitmapImageRep *imageRep = [ [ NSBitmapImageRep alloc ]
initWithBitmapDataPlanes: NULL
pixelsWide:
width
pixelsHigh:
height
bitsPerSample:
bps
samplesPerPixel:
spp
hasAlpha:
YES
isPlanar:
NO
colorSpaceName:
NSCalibratedRGBColorSpace
bytesPerRow:
0
bitsPerPixel:
0
];
NSGraphicsContext *bitmapContext1 = [ NSGraphicsContext
graphicsContextWithBitmapImageRep: imageRep ];
void *graphicsPort = [ bitmapContext1 graphicsPort ];
NSGraphicsContext *bitmapContext2 = [ NSGraphicsContext
graphicsContextWithGraphicsPort: graphicsPort
flipped:
YES
];
[NSGraphicsContext setCurrentContext: bitmapContext2 ];
#endif // USE_IMAGE or BITMAP
NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
NSLog(@"%s currentContext: %@",__FUNCTION__,
currentContext);//<NSSnapshotBitmapGraphicsContext: 0x1018c1e40>
NSAffineTransform *transform = [NSAffineTransform transform];
#ifdef USE_IMAGE
[transform scaleXBy: scaleFactor yBy: +scaleFactor ];
[transform translateXBy: -NSMinX(bounds) yBy: -NSMinY(bounds) ];
#else // USE_BITMAP
// no idea why these have to be different:
[transform scaleXBy: scaleFactor yBy: -scaleFactor ];
[transform translateXBy: -NSMinX(bounds) yBy: -NSMaxY(bounds) ];
#endif // USE_IMAGE or BITMAP
[transform concat];
NSUInteger graphicIndex = [graphics count];
while (graphicIndex-- > 0)
{
SKTGraphic *graphic = graphics[graphicIndex];
[currentContext saveGraphicsState];
[NSBezierPath clipRect:[graphic drawingBounds]];
[graphic drawContentsInView:nil isBeingCreateOrEdited:NO];
[currentContext restoreGraphicsState];
};
#ifdef USE_IMAGE
[image unlockFocus];
NSRect recct = NSMakeRect(0, 0, sizE.width, sizE.height );
[image lockFocus];
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc]
initWithFocusedViewRect: recct ];
[image unlockFocus];
#endif // USE_IMAGE
NSBitmapImageFileType exTyp = isTiff ? NSTIFFFileType : NSPNGFileType;
NSData *data = [ imageRep representationUsingType: exTyp properties: nil ];
Gerriet.
_______________________________________________
Cocoa-dev mailing list ([email protected])
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 [email protected]