0

I'm trying to setup up a NSCollectionView which is binded to an entity and I'm trying to load an image into an NSImageView out of this entity. I stored the image as a NSData object.
I did a little research and found out that I'm supposed to set a ValueTransformer but I couldn't get this work.

Here is my Code for the save process of my image attribute pdfImage:

pdf = NSData(contentsOfFile: path)

            // pdf to image
            let pdfImageRep = NSPDFImageRep(data: pdf)
            let factor: CGFloat = 300/72

            pdfImageRep?.currentPage = 1
            let image = NSImage(size: self.frame.size, flipped: false,
                drawingHandler: {
                    dstRect in

                    (pdfImageRep?.drawInRect(dstRect))!
                    return true
            })

            let scaledImageRep = image.representations.first
            scaledImageRep?.pixelsWide = Int((pdfImageRep?.size.width)! * factor)
            scaledImageRep?.pixelsHigh = Int((pdfImageRep?.size.height)! * factor)

            let pngImageRep = NSBitmapImageRep(data: image.TIFFRepresentation!)
            let imageData = pngImageRep?.representationUsingType(NSBitmapImageFileType.NSJPEGFileType, properties: [:])

            pdfImage = imageData

I save the pdfImage later in the code.

As I said I tried to use NSKeyedUnarchiveFromData as ValueTransformer but it didn't work.

Am I doing it wrong or is there an easier way? And can somebody help me with this problem?

Thank you very much!

Tom Kuschka
  • 463
  • 1
  • 8
  • 17

1 Answers1

0

I store NSImage in Core Data. The type in the data model is transformable.

anImage = [[NSImage alloc] initWithContentsOfURL:aFileURL];
if (anImage) {
    anImageObject = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:[self managedObjectContext]];
    [anImageObject setImage:anImage];
}

I bind the value of NSImageView to the image property without a value transformer.

ps. if you store image data (or other blobs) in Core Data, put the image in a seperate entity or store paths. See the blob section of the Core Data Programming Guide.

Willeke
  • 14,578
  • 4
  • 19
  • 47