views:

128

answers:

2

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.

+2  A: 

The usual way to scale an image is to allocate a new NSImage with the desired smaller dimensions, lockFocus on it, and draw the original image into the NSImage (unlockFocus when 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, the TIFFRepresentation method on NSImage.

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 image property 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.

Alex
+1  A: 

Think NSValueTransformer. A value transformer is just that... it transforms the value. So you want your image resized (eg. transformed) and value transformers work through the bindings. If you look at the bindings in interface builder you'll see the "Value Transformer" section and that's where you'll hook your transformer in. Transformers have 2 main methods that do stuff, a transformedValue method and a reverseTransformedValue method. The first is used when displaying your data in the image view and the second is used in the opposite direction... when data is written to your core data model. So they're the in-and-out methods that are between core data and the interface.

So for your case, you would do your resizing (as Alex explained) in the reverseTransformedValue method. See here for more details and examples of what you need to do.

regulus6633