2014-10-17 51 views
0

要加載的圖標圖像,我有以下代碼中的方法之一:NSImage中-initWithContentsOfFile返回尺寸的圖像的零(0)

NSLog(@"icon path: %@", iconPath); 
NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile:iconPath]; 
return iconImage; 

從日誌輸出,很顯然,圖像的資源是從正確的位置打開。我沒有看到錯誤。然而,我打開的tif文件在調試器中顯示爲空的NSSize(width = 0,height = 0),並顯示在屏幕上,就像我指向一些失控的內存段一樣。

標誌主要設置爲0。例外colorMatchPreferred和multipleResolutionMatching設置爲1。

銷售代表指向包含的陣列(NSArrayM *)兩(2)位圖表示(NSBitmapImageRep條目)。

請告訴我做錯了什麼!

謝謝

+0

請包括完整的異常文本和堆棧跟蹤。 – Droppy 2014-10-17 08:33:25

+0

沒有例外,因此沒有堆棧跟蹤。 – 2014-10-17 08:35:58

+0

好的,我被「異常是colorMatchPreferred和multipleResolutionMatching設置爲1」的說法弄糊塗了。 – Droppy 2014-10-17 08:37:41

回答

0
//to get image original size use this code 

NSArray * imageReps = [NSBitmapImageRep imageRepsWithContentsOfFile:fileUrl]; 
     NSInteger width = 0; 
     NSInteger height = 0; 

     for (NSImageRep * imageRep in imageReps) 
     { 
      if ([imageRep pixelsWide] > width) 
       width = [imageRep pixelsWide]; 
      if ([imageRep pixelsHigh] > height) 
       height = [imageRep pixelsHigh]; 
     }