Make NSView resizable WITHOUT resizing images contained within - objective-c

I have an NSView that resizes automatically 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.

Related

How to change UITableViewCell Image to Circle in UITableView if width and height are different

My code is
imv.clipsToBounds=YES;
imv.layer.cornerRadius=imv.frame.size.width/2;
imv.layer.borderWidth = 4.0;
The best solution would be to either resize your image view to be square (using autolayout or manually set the frame) or even better you could redraw the image using an image context with a circular clipping mask, then you wouldn't have performance problems on older devices because of corner radii that you recompute on scrolling.
Something like here https://stackoverflow.com/a/17722702/384309

NSImageView won't scale image down if frame style is NSImageFrameNone

I need to display NSImageView on resizable NSWindow. The image displayed in the view should scale down if it's too big to fit image view in the widnow and it should also change its size when resizing the window. I am able to achieve correct behavior using Auto-Layout, and setting imageScaling property of NSImageView to NSImageScaleProportionallyDown. Unfortunately it's not working when my image view's imageFrameStyle property is set to NSImageFrameNone (it works for any other option like NSImageFramePhoto or NSImageFrameGrayBezel). I don't want any frame to be displayed, like with NSImageFrameNone, but disabling the frame breaks autoresizing - it looks like with frame set to NSImageFrameNone image is not scaled down and NSImageView is scaled up to match displayed image size. Anyone have solution for that issue?
It occurs that it's easier than I thought. The problem was with Auto-Layout, not with NSImageView scaling logic. Using InterfaceBuilder with my image view selected I had to change "Content Compression Resistance Priority" to lower value (it's located on Size Inspector tab in Xcode5). That solved my problem and the image is now scaled properly when resizing window.

Autoresize Height and Width Proportionally Interface Builder

I have a UIImageView that is set to autoresize it's height based on the height of the Superview, i.e. when the In Call Status bar comes down. How do I make the entire view resize proportionally so that the width of the UIImageView changes when the height changes?
I would like to do this in Interface Builder, but programmatically can be used as well.
Thanks
The easiest way in iOS 6 is to use the (new in iOS 6) autolayout feature. It is very easy to make a view's width always be a fixed proportion of its height.
Otherwise you'll have to detect the change in your view controller's layoutSubviews and use code to resize the UIImageView.
However, consider the alternative of letting image resize rather than the whole image view. If you give the UIImageView the right contentMode, it will automatically resize the image proportionally if the view's height changes.

UIImageView with another UIImage of frame

i m developing an photo based app
i have two imageview one imageview contains the original image
and other imageview contains the frame
Now the problem is i want to manage the frame Imageview according to the main image
i.e. if my main image view is of size 320x480 then border applies on that size
and if my image view is of size 200x150 then border have to applies on that size
How to do that
UIImage.size can give you dimensions of image you are using. you can resize your UIImageView.frame using this size. this will fit UIImage in UIImageView without any extra space so borders will appear perfect.
second option could be to initialize an image view once again using following method.
- (id)initWithImage:(UIImage *)image
this will make a perfect sized imageview for your image.
Let me know if you are not looking for something like this....

Resizing CAShapeLayer

I have a simple test app on which my rootViewController's UIView contains a bunch of UIView subviews. Each one of those UIView subview is backed by a CAShapeLayer.
I want the composition created by those subviews ( the four shapes that are within the dotted area .. ) to always stay vertically and horizontally centered with respect to my
UIWindow. (the minimium size of the left/right, top/bottom margins will be subject to be changed at runtime at each orientation change )
So for example when i rotate to portrait i will have to
resize and reposition those single shapes so that the whole figure will be mantained centered and each CAShapeLayer sublayer stays sharp ( i want their path to be resized not just a raster resize )
what would be the best technique to resize/move the shapes to always have a centered composition while maintaining path crisp appearance for the shapes?
Ultimately for me it will be good to have an answer to this: how can i shrink the subviews as a whole? i mean their sizes and relative positions?
Thanks
You can use CGPathCreateMutableCopyByTransformingPath() or -[UIBezierPath applyTransform:] to recalculate all points in the path.