nosedive25
nosedive25

Reputation: 2435

Write NSImage to file

I have an NSImage from NSImage *myImage = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[outputView bounds]]; and I need to save it to a file. I havent been able to find anything about saving NSImage in any format. Has anyone done this? Is it even possible?

Thanks

Upvotes: 0

Views: 2131

Answers (3)

Peter Hosey
Peter Hosey

Reputation: 96323

If you're creating a snapshot of an actual view (as opposed to an image that you've locked focus on), then an alternative to creating a bitmap image rep would be to ask the view for PDF data of the desired rectangle. This will be vector rather than raster (except where the view itself draws an image), which will scale more nicely to higher resolutions. You would then write that data to a file the same as you would any other data.

Upvotes: 1

Peter Hosey
Peter Hosey

Reputation: 96323

Having an NSBitmapImageRep (as JWWalker pointed out, that code doesn't create an NSImage instance), you can ask the image rep for a CGImage version of itself, and then create a CGImageDestination to write that image to a file. This may be more efficient than obtaining a data object (which will hold the raster data in memory) and provides more options.

Upvotes: 1

JWWalker
JWWalker

Reputation: 22707

The way you say you're making an NSImage doesn't make sense. You show how to create an NSBitmapImageRep, not an NSImage.

Before you save it to a file, you convert to NSData. There is an NSImage method to convert to TIFF data, and there is an NSBitmapImageRep method to convert to data in several formats.

Upvotes: 2

Related Questions