8

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

Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81
Sam
  • 6,215
  • 9
  • 71
  • 90

2 Answers2

6

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);
Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81
  • Thx for quick reply, can you tell me how to convert the below code to Mac OS X Iphone App-->using UIKit CGRect rect = CGRectMake(offset.x * scale, offset.y * scale, scale * size.width, scale * size.height); UIGraphicsPushContext(ctx); [image drawInRect:rect]; UIGraphicsPopContext(); – Sam Apr 21 '10 at 11:56
  • 1
    Take a look at the Drawing Guide (http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/CocoaDrawingGuide/GraphicsContexts/GraphicsContexts.html). It explains how to get a graphical context and how to draw images. – Laurent Etiemble Apr 21 '10 at 13:15
2

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

Williham Totland
  • 28,471
  • 6
  • 52
  • 68
  • Thx for quick reply, can you tell me how to convert the below code to Mac OS X Iphone App-->using UIKit CGRect rect = CGRectMake(offset.x * scale, offset.y * scale, scale * size.width, scale * size.height); UIGraphicsPushContext(ctx); [image drawInRect:rect]; UIGraphicsPopContext(); – Sam Apr 21 '10 at 11:57