2013-02-18 58 views
4

我正在開發適用於Mac OS X的應用程序。我試圖將任何NSImage調整爲像200x300這樣的特定大小。 它非常適合非視網膜蘋果機。 但是對於Retina Mac,正如我們預期的那樣,它將圖像大小調整爲400x600,這只是雙倍。 我的項目需要根據給定的尺寸調整圖像大小,而不管運行應用程序的設備是視網膜還是非視網膜。 我該如何達到目標。調整視網膜設備上的圖像大小

我試圖衡量規模的屬性,至於視網膜的規模是2.0,所以我們調整圖像的一半所需的。

但是,當我們連接到顯示器,一個是視網膜和其他不是視網膜,它再次產生相同的問題。

這裏是我已經用於調整大小的代碼:

-(void)resizeImage:(NSImage *)sourceImage newSize:(CGSize)newSize 
{ 


    NSString *newImagePath = [[[Utility documentPath] stringByAppendingPathComponent:imagefolder] stringByAppendingPathComponent:@"imageName"]; 



    [sourceImage setScalesWhenResized:YES]; 
    [sourceImage setSize:newSize]; 


    NSImage *newImage = [[NSImage alloc] initWithSize:newSize]; 

    [newImage lockFocus]; 

    NSRect frame = NSMakeRect(0, 0, newSize.width, newSize.height); 

    [NSGraphicsContext saveGraphicsState]; 

    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:frame xRadius:0 yRadius:0]; 
    [path addClip]; 

    [sourceImage drawInRect:frame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0]; 

    [NSGraphicsContext restoreGraphicsState]; 

    [newImage unlockFocus]; 

    CGImageRef CGImage = [newImage CGImageForProposedRect:nil context:nil hints:nil]; 
    NSBitmapImageRep *imgRep = [[[NSBitmapImageRep alloc] initWithCGImage:CGImage] autorelease]; 


     NSData *data = [imgRep representationUsingType:NSPNGFileType properties: nil]; 
     [[NSFileManager defaultManager] createFileAtPath:newImagePath contents:data attributes:nil]; 

    [newImage release]; 

} 

在此先感謝。

+0

你的問題是什麼?在我看來,在調整圖像大小時恰恰考慮了規模,這正是您想要實現的目標。 – trojanfoe 2013-02-18 13:10:35

+0

我只是想根據給定的尺寸調整圖像的大小,而不管我們運行應用程序的哪個設備(是視網膜還是非視網膜) – iPhoneDv 2013-02-18 13:18:31

+0

您的意思是點的大小相同嗎? – paulmelnikow 2013-02-18 15:56:45

回答

5

我通過將目標大小除以屏幕比例(screenScale)來解決此問題。 ScreenScale在普通屏幕上爲1.0,在視網膜屏幕上爲2.0:

CGFloat screenScale = [[NSScreen mainScreen] backingScaleFactor]; 
float targetScaledWidth = sourceImage.size.width*scale/screenScale; 
float targetScaledHeight = sourceImage.size.height*scale/screenScale; 
+0

跑進同樣的問題!感謝您的解決方案! – spacecash21 2014-01-17 15:10:22

+0

@Gros:感謝您的精彩解決方案!你有沒有檢查4K和5K視網膜顯示器? – Aravindhan 2015-11-04 06:16:59

+0

我沒有這樣的屏幕來檢查它是否工作(它應該)。有人可以確認嗎? – Gros 2015-11-05 14:38:39

相關問題