开发者

Make NSView resizable WITHOUT resizing images contained within

I have an NSView that resizes au开发者_C百科tomatically based on the size of the window. I load images and display them in the NSView, but I do not want the NSView to try and "squeeze" them to fit the NSView's size. What I would like is to have the images load into the NSView at 100%, then resize the window to reveal more of the image rather than scale it to fit. I may want to modify this later to allow zooming and panning as well.

Here's what I did:

-(void)drawRect:(NSRect)rect
{
    NSRect theRect = NSMakeRect((rect.size.width/2)-(([theImage pixelsWide]/2)/2),
                                (rect.size.height/2)-(([theImage pixelsHigh]/2)/2),
                                [theImage pixelsWide]/2,
                                [theImage pixelsHigh]/2);

    [theImage drawInRect:theRect];
}


How are you displaying the images in the first place? Either you're using an NSImageView or you're drawing the images into your custom view yourself in -drawRect:.

If you're using an image view, take a look at its autosizing settings (its geometry as its parent view's geometry changes) and its image scaling properties (the image size and ratio as the image view's geometry changes.

If a custom view then draw the image into whatever rectangle you please rather than the view's entire bounds.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜