4

My target is to display an image in a view. Considering I've an:

  • IBOutlet NSImageView *image for display my image
  • NSData *imageData for readig the image file
  • NSImage *imageView

In imageData is stored the image (I've used initWithContentsOfFile method).

Now, if I initialize imageView with:

NSImage *imageView = [[NSImage alloc] initWithContentsOfFile:path];

I can see the rendering of the image correctly, but in this way I'm reading twice from the file system.

If I try to:

NSImage *imageView = [[NSImage alloc] initWithData:imageData];

The displayed image is very small..like thumb

Whit CGImageRef:

CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
CGImageRef imageRef= CGImageSourceCreateImageAtIndex(imageSource, 0, nil);
NSImage *imageView = [[NSImage alloc] initWithCGImage:imageRef size:NSZeroSize];

The image is still rendering too small.

How can I display the image in original resolution?

Matteo
  • 41
  • 1
  • 3
  • if `initWithContentsOfFile:` works for the `NSImage`, why do you still need the `NSData`? (but I agree that it's strange -- could be an image representation or sizing issue.) – Richard Mar 14 '11 at 13:51
  • I've edit my post: added imageSource – Matteo Mar 14 '11 at 14:02
  • I've a NSData for reading exif tags. – Matteo Mar 14 '11 at 14:04
  • @Joe Blow: I don't think changing the dpi could be the solution. The original resolution is about 2800x1300 and the result is about 100x70. – Matteo Mar 14 '11 at 14:19
  • I think I'm getting only a thumb and not the full image..but I have no idea how to retrieve the portion of NSData where the real image is stored. – Matteo Mar 15 '11 at 08:24
  • @Joe Blow: sorry for my presumption, I've read only today that there is a NSImageRep class for representations of an image. I've tried to use it, but I'm still receiving a little image for a RAW image (for jpeg all it's working fine). I'm using NSBitmapImageRep (initialized from NSData) then, I add the representation to NSImage (initialized with size of rep). Any suggestion? – Matteo Mar 24 '11 at 14:38
  • I'm having the same problem. I'm initializing my NSImage with data retrieved from a URL connection. They show up smaller than they do in the web browser. Did you find a fix? – Arlen Anderson Apr 22 '11 at 09:50
  • Not yet..I'm still finding a solution. If you'll fix this, please post the solution here. Thanks. – Matteo Apr 25 '11 at 09:19

1 Answers1

5
NSImage *imageView = [[NSImage alloc] initWithData:(NSData *)imageData];
Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93
  • Thanks for response. I'll try soon as possible and I'll feed you back. – Matteo Oct 07 '11 at 08:33
  • Sorry if I reply you late. When I paste your code in my project the compiler says _error: Semantic Issue: Unknown receiver 'UIImage'; did you mean 'CIImage'?_ If I change UIImage to CIImage the compiler says me _warning: Semantic Issue: Incompatible pointer types initializing 'NSImage *' with an expression of type 'CIImage *'_ and if I try to run the compiler say _Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_CIImage", referenced from: objc-class-ref in ButtonsAppDelegate.old: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1_ – Matteo Jan 07 '12 at 12:28
  • 1
    Change UIImage to NSImage. He probably typed that by accident. – tallen11 May 19 '12 at 20:24
  • imageWithData method does not exist for NSImage. – Vibhor Goyal May 20 '13 at 05:39
  • Worked great for me on Mavericks. Proof in real world code: https://github.com/HermesApp/Hermes/commit/19302fdbc2e7a8503bb34912aa567a134dc4a1f3 – Winny Apr 16 '14 at 02:17
  • 1
    Code example corrected, had typo and should indeed have been initWithData: not imageWithData: – Duncan Babbage May 26 '14 at 00:54