Re: Scaling an NSImage to have exact pixel dimensions
Re: Scaling an NSImage to have exact pixel dimensions
- Subject: Re: Scaling an NSImage to have exact pixel dimensions
- From: Carl Hoefs <email@hidden>
- Date: Fri, 10 Oct 2014 09:23:14 -0700
On Oct 10, 2014, at 1:02 AM, Graham Cox <email@hidden> wrote:
>
> // ... set up destination context ...
>
> [[NSGraphicsContext currentContext] setImageInterpolation: NSImageInterpolationHigh];
> [sourceImage drawInRect:dest fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0 respectFlipped:YES hints:nil];
>
> // ... clean up leaving NSBitMapImageRep alone...
>
> NSData* imageData = [destBitMapRep representationUsingType:... properties:…];
Thanks Graham and Ken! You’re right, no need at all for the CIImage (flail) step. Here’s my cleaned-up code, combining both your inputs, works perfectly. Anything still superfluous here?
-Carl
. . .
NSRect imgRect = NSMakeRect(0.0, 0.0, 640.0, 480.0);
NSBitmapImageRep *offscreenRep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:imgRect.size.width
pixelsHigh:imgRect.size.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bitmapFormat:NSAlphaFirstBitmapFormat
bytesPerRow:0
bitsPerPixel:32];
NSGraphicsContext *gc = [NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:gc];
[[NSGraphicsContext currentContext] setImageInterpolation: NSImageInterpolationHigh];
[srcImage drawInRect:imgRect fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0 respectFlipped:YES hints:nil];
[NSGraphicsContext restoreGraphicsState];
NSData *dataObj = [offscreenRep representationUsingType:NSJPEGFileType properties:nil];
. . .
* Final NSImage: <NSImage 0x608000073f80 Size={640, 480} Reps=(
"NSBitmapImageRep 0x6080000a9d20 Size={640, 480} ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) Pixels=640x480 Alpha=NO Planar=NO Format=(not yet loaded) CurrentBacking=nil (faulting) CGImageSource=0x60800016e700"
)>
_______________________________________________
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