This was a question asked by a user on Apple's cocoa-dev mailing list, who thought my answer would be helpful on Stack Overflow:
In XCode/IB, if add an Image View that has the Image property to an
image in my project it shows the image scaled to fit the Frame of the
Image View. How can I stop this and just truncate it? I want two
images on top of each other and just show the top half of one and the
bottom half of the other.
I see loads of scaling options in the “Content Mode” Property, but
nothing to say “none”. The closest I’ve managed so far is to set the
Content Mode to “Aspect Fit” and also set the clip to bounds. This
does what I want BUT the centre of the image shows, not the top, left.
Basically, I have two 24 x 24 images placed one on top of the other, I
want to show the first half (e.g. 0,0,24,12) of ImageA and the bottom
half of imageB (e.g. 0,12,24,12).
Can I do this in XCode/IB? If not can I do this in code?
Create two image views, set their sizes to 24 x 12, and set the Clip To Bounds property.
For one view, set its content mode to top (== top-center) or top-left, and the other to bottom (== bottom-center) or bottom-left.
If you turn off the clip property then you'll see that the images draw outside the image view's bounds.
Related
I want to know how can i use this property for UIImageView.
Stretching properties are pretty simple, as stated by Karol Kozub in this article:
The fraction of the original image left without stretching on the left
is specified by X
The fraction of the original image that gets stretched in the x-axis
is specified by Width
The fraction of the original image left without stretching on the
right is equal to 1 – X – Width
If we use 0 for Width the stretched area will interpolate between
the last pixel of the left part and the first pixel of the right part
The y-axis works analogously
This sets the contentStretch property for views (this is a UIView property, not specifically a UIImageView property). This property has been deprecated since iOS 6, however, so you shouldn't use it.
The replacement, specifically for images, is resizableImageWithCapInsets. The normal use of this is to create an image with a left and right side (or top and bottom), and a single-pixel wide "middle" that is stretched across the view. It's common for custom buttons both because it's flexible to a variety of widths, and because it saves some space.
See "Defining a Stretchable Image" in the UIImage docs for full details.
I have an image that I want to align with the bottom right corner of the View and then I want the rest of the image to scale to fill the available space but without falling off screen.
My image is a square, so I don't want the width of the image to be any greater than the device's shortest side (portrait or landscape).
I'm new to Size Classes, but no matter what constraints I apply, the image grows bigger than the view and seems to have a gap between the bottom and right had edges of the screen, even though I've set those constraints to 0.
Your specifications are a little unclear, which may account for the difficulty you're having; you need to be very clear on exactly what you want. I had no trouble making a square image be square and fill the shorter dimension exactly in both orientations:
So if that's what you want, it's easily achieved with constraints (no size classes involved).
I have simply dragged UIImageView into storyboard and made it square. I added a pink background to show the effects of the leftover space in the ImageView. In each case I added either a taller image (1st image) and a wider image (2nd image), as well as a text label. Here are my results.
So the obvious question is....how can I get rid of this extra (pink) space and keep the integrity of the photo (that is, to not have to stretch or lose part of the image)? If I wanted to be able to scroll through photos, it would be nice to have them all the same width to the edge so they look neat and orderly (if they were portrait), and if I wanted to have text under each, I'd want the text to be closer to it, rather than have all the blank (pink) space in between if it were landscape. And obviously different size images will give different sizes of blank space.
So I'm thinking what I could do is before displaying the image, get the size of it, then just have a designated distance from either the label or the edge of screen, depending on the orientation of the picture, and then creating/changing the size of the UIImageView with a bit of math and using the image dimensions before inserting the picture into the ImageView. Is this possible? Is there another method I can't quite figure out?
Just look at any decent photo app and they are nice and neatly organized/displayed despite being different sizes, orientations, etc and I'm wondering how to pull this off. I obviously haven't gotten too deep into using images past simply showing them in a pre-determined ImageView.
Thanks for the help/suggestions!
Try this... set your UIImageView to AspectFit (not AspectFill since that will lose some of the image) and using constraints do the following:
centre the UIImageView in the container both horizontally and vertically
set the UILabel to float below the UIImageView by whatever distance you desire ("standard" is usually good)
set the left, right, and top constraints on the UIImageView to be >= whatever distance you desire
set the bottom constraint on the UILabel to be (once again) >= whatever distance you desire
The effect of this should be that the UIImageView will properly resize itself to its intrinsic size and the constraints should properly position it and the label.
i have to add a number images to the view. the images i have from the designer are of correct size. it would speed up my work if i don't have to manually size each image-view as per size of the image and then select the image ( which anyways contains the right dimensions )
is there a way in which i can put an image-view of any size select image property and then select an option that would resize the image-view as per image
You can set their content mode to a position like center, left, right, top, bottom... That way the image will keep its original size.
You just need to make sure that the imageview is large enough to display the image, so it doesn't get cropped.
I don't know exactly what your designer provides you in terms of position of the elements, but it's probably the top left position, so use the Top Left mode so that the image within the imageview hugs the top left corner and keeps its original size.
I am developing a universal app for iOS-7. this particular question is specific to iPad only. I want to place an image as background of root view of a View Controller i.e. the image should fill the whole screen. The 1x image has size:768x1024. This works for Portrait orientations as non-retina resolution of iPad is also 768x1024 in portrait. In Landscape however the image does not fit. I have tried using ScaleToFit but since aspect ratio of image is not preserved I can not use ScaleToFit(their are things in image which look odd when not scaled proportionally in both axis). AspectFill resizing seems most suitable for my need, but their is a small problem. As defined in Apple Documentation "The content is resized to completely fill the bounds rectangle, while still preserving the aspect of the content. The content is centered in the axis it exceeds.". I do not want the content to be centered in axis in which it exceeds, I want it to be aligned to top/left edge.
So basically I want two things:
Aspect Fill
The content remains aligned on the Left/Top edge.
Is it possible to achieve this. Any code-snippet will be great.
Thanks
You will have to subclass the View Controller's view and manually scale and align.
Look at the code snippets in THIS answer.
Adjust imageViewXOrigin and imageViewYOrigin to align however you want.
Really the only way to get around the content fill mode is to have two different images, one for each orientation. I'd suggest changing the image in the view controllers willAnimateToOrientation: method so that you can put the image changes inside UIKit's animation block.