How to read PNG image to NSImage

狂风中的少年 提交于 2019-12-09 11:13:36

问题


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


回答1:


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);



回答2:


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



来源:https://stackoverflow.com/questions/2681630/how-to-read-png-image-to-nsimage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!