I am working on application to keep a comic book collection in order. The user should be able to drag an image of the cover artwork into the program via an ImageWell.
Since it is not possible to drag the image out of the application again I don’t need to save the picture in it’s original size. An image at the size of the ImageWell would be just fine.
The question is how do I rescale the image with my application?
To make things even more complicated the ImageWell is bound with Core Data. So I need to rescale the image before Core Data will save the picture in its original size.
The usual way to scale an image is to allocate a new
NSImagewith the desired smaller dimensions,lockFocuson it, and draw the original image into theNSImage(unlockFocuswhen you’re done, of course). From there, you can serialize the image in a variety of formats using either the CoreGraphics APIs or, if your needs are simple, theTIFFRepresentationmethod onNSImage.As for dealing with Core Data, I’d recommend keeping your image in a separate entity with a relationship to the object that owns it. The reason is because Core Data loads all the data for an object at once when fetching, so when you don’t need the image data (which can be quite large, even for small images) you can avoid the slow performance and memory pressure of loading it into memory on every fetch. You can put a transient
imageproperty on the main entity that lazily loads/stores the image as needed.When your image well updates its binding with the new image, that would be the perfect opportunity to rescale and store the image in your data object. That is, you’ll pass the full size image to the data object through binding, and it will handle the rescaling.