2013-02-18 4 views
4

Mac OS X 용 앱을 개발 중입니다. NSImage 크기를 200x300과 같은 특정 크기로 조정하려고합니다. 비 망막 Mac에서는 잘 작동합니다. 그러나 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

+0

난 우리가 응용 프로그램을 실행하는 장치 (망막 또는 비 망막)에 상관없이 주어진 크기에 따라 이미지의 크기를 조정하고자했습니다. – iPhoneDv

+0

같은 크기의 포인트를 의미합니까? – paulmelnikow

답변

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

+0

@Gros : 멋진 솔루션에 감사드립니다! 4K 및 5K 망막 디스플레이에서 이것을 체크 했습니까? – Aravindhan

+0

작동하는지 확인하는 화면이 없습니다. 누가 확인할 수 있니? – Gros