issues in resizing UIImage on IB - ios

I am facing a strange issue while resizing images in IB (Xcode 5). First, I drag & drop a UIImageView on my viewController in a storyBoard. I then resize and set an image for it, which works properly. However, once I try to resize the UIImageView control after setting the image, it starts behaving strangely. Sometimes it occupies the complete viewController, other times the image becomes so big that it is out of bounds.
I tried to set a smaller image, but it resulted in more strange behavior. Now if I try to resize it, sometimes it can't be resized by dragging. Sometimes the UIImageView is resizes properly but the contained image remains the same size. Any suggestions?
This was not happening when using Xcode 4.

There are two issues here.
For an image to be resized to the same size as its image view, it is essential that its content mode be set to UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFit, or UIViewContentModeScaleAspectFill. You can make this setting in the attributes inspector.
NOTE: If you do not do this, then the image will be shown at its own natural size. If the image is huge, it will be shown huge. Moreover, it will be shown huge even if the image view is small, because the image view does not clip its image. If you want to the image view to clip its image (so that you can see where the bounds of the image view really are), set the image view's Clips To Bounds (clipsToBounds). Again, you can make this setting in the attributes inspector.
In Xcode 5, you are using auto layout by default. This means that the image view has the size of its image by default. If you don't want that, you must use constraints to set the height and width of the image view. You will then probably also have to use constraints to set its position. Do NOT run the app until all auto layout warnings in Interface Builder have been taken care of, or you will get a big surprise at runtime.

Related

Resize an image that is in a button

I have a button with an image inside, but I want to adjust the size of this image, how can I adjust the size of the image?
I'm trying to make my image 150 points high, but I do not know how to do it programmatically
The best possible approach would be to regenerate the image asset at the appropriate size you need it (using photoshop, preview, etc) and then use that instead.
Another option, in the storyboard switch to the ruler tab and adjust the image insets.
Yet another approach would be to use a UIImageView and add a UITapGestureRecognizer to it to detect a tap. Then you can configure the image view with constraints and set up the aspect ratio. The downside here is that you lose the stateful changes of the button when it is pressed/depressed so it won't look as good. I wouldn't add an imageview as a subview of a button either, that can create strange problems.
One thing I'd like to add is that resizing images can be expensive. If you are loading these buttons into a tableview then you need to be careful not to accidentally resize the images as the cells are enqueued/dequeued. This will block the main thread and cause smooth scrolling issues.
When you set a button’s image it will have an imageView property containing the image. Therefore, you can set the imageView’s contentMode property to .scaleAspectFit to keep the image proportions after resizing:
button.imageView?.contentMode = .scaleAspectFit
The actual resizing can be done in InterfaceBuilder using layout constraints, for example.
However, if you know the button target size at compile time, you should make sure the image already has the desired size instead of sizing a wrongly-sized image at runtime.

How to make an image in LaunchScreen.xib fill the background using Xcode

So I'm trying to setup a full screen background with a logo image centered in its middle with an existing LaunchScreen.xib (for a react native project) and I am having trouble with the autoformat.
I see that I can add constraints to my ImageView to maintain aspect ratio but I'm not sure how to make it fill(crop to fit) the view. When I test on device it just stays the same square as the interface builder and doesn't fill the screen. I've been trying to add different constraints to the image but it doesn't seem to affect it.
Also how do I preview it on different devices as shown in the UI. Selecting different devices doesn't seem to change the square canvas view at all.
BTW I don't want to do it as separate assets for every screen size - I'm curious to learn how to build it using the standard xib interface builder using layout rules.
The xib file was shown by a view that inside a UIViewController.
As you set constraints for UIImage in xib file (e.g. in UIView) you should set constraints of UIView inside the UIViewController.
Then now set constraints of root view to the edges, and set Content Mode of imageView to what you want as apple document said:
The scaleAspectFit and scaleAspectFill modes scale the image to fit or fill the space while maintaining the image’s original aspect ratio. The scaleToFill value scales the image without regard to the original aspect ratio, which can cause the image to appear distorted. Other content modes place the image at the appropriate location in the image view’s bounds without scaling it.

Button image stretching

I am having a problem with the button image. I have a green background and an icon. The problem is that on larger screens the image stretches due to the button also stretch to the size of the screen.
I found several other topics they were talking about something, but none of them helped me solve this problem.
The next best picture explains the problem:
If you want your button to look like the 3rd image, you will have to use an image of that size and resolution.
Alternatively, you can set contentMode on your UIButton's imageView object. Use this line of code in your viewDidLoad function
button.imageView!.contentMode = UIViewContentMode.ScaleAspectFit
When your constraints increase the size of your button, the image used inside it will abide by the contentMode property set. Using those you can stretch it, which it's doing right now, or you can keep the aspect ratio, in which case it will show empty space on sides.
Play around with the content mode settings, try 'Aspect fit' instead of the currently selected.
It look like your constraints doesn't take on consideration the size of the asset.
You should set a Width and a Heigth constraints to your image asset.

UIButton background image not sizing right with autolayout?

I'm simply trying to fill the entire width of an iPhone screen with a login button for my users that is basically an image set as the background of a UIButton. This is the result I end up with (Notice it fills width as intended, but shrunken down in height):
My thought process was to make 3 images to compensate for #1x #2x #3x resolutions with different image sizes?
#1x: 144x33px
#2x: 288x65
#3x: 432x97
Now from there I thought I could just fill the width of my ViewController with the UIButton in Storyboard and set the background image of the UIButton which I would think would use the proper one automatically depending on device considering I put them in the right spots in Images.xcassets. I also set the trailing and leading constraints as well as bottom constraint to keep it filling left to right and stick to bottom. I just don't get how I would fix the height issue if it's looking fine in the actual XCode storyboard, but not in the preview or simulator as seen in above photo.
Any help would be tremendously appreciated.
The different image sizes are not used to replace each other when you scale an image. They are used to look nice when there are more actual pixels than software pixels.
For your purposes, you probably want to scale the image with the stretching rules or cap insets.
Check out this answer for an example:
https://stackoverflow.com/a/13367861/1864591

Image not displayed perfectly on table cell

I use auto layout and building app for iOS 7. I am using a custom UITableViewCell. I want to display an UIImageView inside it with some margin from contentView's borders. So I have taken an UIView which has margin from contentView's borders. UIImageView that I want to display is put into this UIView with all edges touching its superview.
Now, issue is the UIImage (green background) which is set to UIImageView is not displayed perfectly. By perfectly I mean that it is displayed but bottom 1px is not displayed. I have set image in storyboard and it looks perfect there as shown below.
But when I run the app then it looks as shown below. Concentrate on bottom black line which is clearly visible in storyboard but not when I run the app.
Please note that cell height in storyboard is 95pts. Image height is 77pts (non-retina). Also when I scroll cell up and down then I see that during scrolling this bottom line is visible, but that time top lines is not properly visible.
I have noticed such sub-pixelization issues when the simulator is in less than 100% scale size.
Especially on scrolling, when you notice it's sharpness fluctuates, it basically indicates that it's not being rendered properly.
So, test this scenario in the iOS simulator in 100% scale size
command+1
or...
simulator menu > window > scale > 100%.
Alternatively, test this scenario on a device as well since it'll be more "realistic".
If the issue persists then give even numbers in the frame size and/or avoid float values *(but don't quote me on this)
I had the same issue with autolayout and CustomCell with a specific image.
What I did was ticking clip subviews, and the problem was gone. You can tick it in IB by checking imageView and go to Attributes Inspector:
Considering that your autolayout is correct, this question may help.
But anyways give it a try and give me feedback

Resources