我試圖加載一個圖像到我的NSImageView/NSScrollView並顯示在實際大小,但圖像神祕地結束顯示在大約一半的大小。我首先想到它可能被縮小以適應框架等的某種約束,但很快意識到這是不對的,因爲如果我在物理上放大圖像大小(在圖像編輯程序中)然後加載它再次,我發現我可以裝載/顯示儘可能大的圖像。在NSScrollView縮小圖像大小
有問題的圖片的實際大小隻有2505 x 930,我猜這不是問題,因爲我可以將這個值翻倍並翻倍,而不會出現任何明顯的問題(除了當然,它們都被縮小了大約顯示時爲50%)。我很簡單的代碼的相關部分是:
- (IBAction)openSourceImage:(NSString*)aFilepath
{
// obtain image filepath passed from 'chooseFile'...
NSImage *theImage = [[NSImage alloc] initWithContentsOfFile:aFilepath];
if (theImage)
{
[theImageView setImage:theImage];
// resize imageView to fit image; causes the surrounding NSScrollView to adjust its scrollbars appropriately...
[theImageView setFrame:
NSMakeRect([theImageView frame].origin.x, [theImageView frame].origin.y, [theImage size].width, [theImage size].height)];
[theImageView scrollRectToVisible:
NSMakeRect([theImageView frame].origin.x, [theImageView frame].origin.y + [theImageView frame].size.height,1,1)];
[theImage release]; // we're done with 'theImage' we allocated, so release it
// display the window title from the filepath...
NSString *aFilename = [aFilepath lastPathComponent];
[[theImageView window] setTitle:aFilename];
}
}
誰能告訴我,我要去哪裏錯在這裏,以及如何以實際尺寸顯示圖像?
解決方案:好了,所以調用「大小」的結果所顯示的圖像太小用,並呼籲在不確定規模的擴大像「pixelsHigh/pixelsWide」結果工作...
我測試應用程序使用一對滑塊驅動的「十字線」來繪製圖像上的要素座標(例如照片或地圖)。純粹的機會,我意外地注意到,雖然加載的圖像只顯示其實際大小的一小部分,但x,y座標DID對應於實際壽命(指示大約70像素/英寸)。去圖一個...
使用:
[theImage setSize:NSMakeSize(imageSize.width * 4, imageSize.height * 4)];
我現在可以在一個已知的放大倍率來加載圖像,並以相同的因數降低我繪製的X,Y的測量。我還投入了NSAffineTransform方法,以便我放大/縮小以獲得最佳觀看大小。
唷!這對我初級階段的人來說具有挑戰性,而且我仍然不明白原始顯示問題的根本原因,但我認爲最終結果就是所有這些。再次感謝你們兩位:-)
爲了進一步縮小這個問題,我運行了一些更多的測試,發現當我在桌面上的圖像上選擇「獲取信息」時,實際尺寸爲前面提到的2505 x 930。但是,在以上述代碼打開時記錄圖像尺寸*時,尺寸顯示爲601.2 x 223.2(比實際尺寸小約4倍)。所有其他圖像都會出現縮小 - 儘管「縮放」差異不盡相同 - 所以我很滿意代碼沒問題,但實際大小和我調用NSSize size = [theImage size]時報告的大小之間確實存在不一致在裝載期間。 – Bender