Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read PNG image to NSImage

Tags:

cocoa

nsimage

how can i read PNG image to NSImage. I tried the following way,but when i get the width and size of the image i'm getting some weird value.. if any one can direct me in right path.. highly appropriate..

 NSImage * picture =  [[NSImage alloc] initWithContentsOfFile: [bundleRoot stringByAppendingString:tString]];

 NSLog(@"sixe %d %d",picture.size.width, picture.size.height);
 if( picture ){ 
  NSLog(@"Picture is not null"); 
 }else {
  NSLog(@"Picture is null.");
 }

Thanks

like image 653
Sam Avatar asked Apr 21 '10 09:04

Sam


2 Answers

Your code to load the image is correct.

The code to display the size is incorrect; NSSize's members are CGFloat which should be print with the %f format string:

NSLog(@"size %f %f",picture.size.width, picture.size.height);
like image 170
Laurent Etiemble Avatar answered Dec 09 '22 22:12

Laurent Etiemble


Use [NSImage imageNamed:tString]. tString should be the base file name of the image file; and need not contain a file extension.

like image 21
Williham Totland Avatar answered Dec 10 '22 00:12

Williham Totland