On Oct 9, 2014, at 11:57 PM, Carl Hoefs <newsli...@autonomy.caltech.edu> wrote:
> On Oct 9, 2014, at 8:05 PM, Ken Thomases <k...@codeweavers.com> wrote: > >> Create an NSBitmapImageRep with the appropriate properties. Create an >> NSGraphicsContext from that bitmap image rep using +[NSGraphicsContext >> graphicsContextWithBitmapImageRep:]. Make that graphics context current. >> Draw the image. Flush and restore the graphics context. Get the data from >> the bitmap image rep in an image file format using >> -representationUsingType:properties: or (if you really want TIFF) >> -TIFFRepresentationUsingCompression:factor:. > > Awesome pointers, I got it to work! The tricky part was the actual scaling. > It would crop but not scale, so resorted to using CIImage. If there’s a > cleaner way, please let me know! What did you try? If you are starting with an NSImage, you can just draw it using one of the -drawInRect:… methods. > NSRect imgRect = NSMakeRect(0.0, 0.0, 640.0, 480.0); > NSSize imgSize = imgRect.size; > NSBitmapImageRep *offscreenRep = [[NSBitmapImageRep alloc] > initWithBitmapDataPlanes:NULL > pixelsWide:imgSize.width > pixelsHigh:imgSize.height > bitsPerSample:8 > samplesPerPixel:4 > hasAlpha:YES > isPlanar:NO > > colorSpaceName:NSDeviceRGBColorSpace > > bitmapFormat:NSAlphaFirstBitmapFormat > > bytesPerRow:8*4*imgSize.width I recommend that you pass 0 for bytesPerRow. This lets the system optimize the buffer alignment. (It's also one less thing to get wrong.) > bitsPerPixel:32]; > // set offscreen context > NSGraphicsContext *gc = [NSGraphicsContext > graphicsContextWithBitmapImageRep:offscreenRep]; > [NSGraphicsContext saveGraphicsState]; > [NSGraphicsContext setCurrentContext:gc]; > > // scale the image from cgRect down to imgRect > CIImage *ciImage = [CIImage imageWithData:dataObj]; > CGRect cgRect = [ciImage extent]; > [[gc CIContext] drawImage:ciImage inRect:imgRect fromRect:cgRect]; > > // reset the current context > [NSGraphicsContext restoreGraphicsState]; > > // create an NSImage and add the rep to it > NSImage *img = [[NSImage alloc] initWithSize:imgSize]; > [img addRepresentation:offscreenRep]; > NSLog(@"* Final Image: %@",img); Why did you create an NSImage? Just for testing? I was under the impression that you wanted image-file-format data. > Thanks again! You're welcome. I'm glad I could help. Cheers, Ken _______________________________________________ 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