5

NSImageMac OS X 用のアプリを開発しています。200x300 などの特定のサイズにサイズを変更しようとしています。Retina 以外の Mac では正常に動作しています。しかし、Retina Mac の場合、イメージのサイズが 400x600 に変更されます。これは、予想どおり、ちょうど 2 倍です。私のプロジェクトの必要性は、アプリケーションが実行されているデバイスが網膜であるか非網膜であるかに関係なく、指定されたサイズに従って画像のサイズを変更することです。どうすれば目標を達成できますか。

スケール プロパティを測定しようとしました。網膜のスケールは 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];

}

前もって感謝します。

4

1 に答える 1