Map NSImage from NSData

My goal is to display an image in a view. Given that I have:

  • IBOutlet NSImageView * image to display my image
  • NSData * imageData to read an image file
  • NSImage * imageView

The image is stored in imageData (I used the initWithContentsOfFile method).

Now, if I initialize imageView with:

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

I see the image rendering correctly, but this way I read twice from the file system.

If I try:

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

The displayed image is very small .. like a 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 too small.

How can I display an image in its original resolution?

+4
source share
1 answer
 NSImage *imageView = [[NSImage alloc] initWithData:(NSData *)imageData]; 
+5
source

Source: https://habr.com/ru/post/1343630/


All Articles