Calculating the best image size for a UITableViewCell - ios

In my app I have table view cell that is sized at 600x300, and I want to put a background image in each cell that takes up the entire space. I thought I could just throw a 600x300px image into it and it would look fine but the images don't look the best.
So I was wondering how if there were some calculation I could use to find the best image resolution for a TableViewCell, that I could apply to my situation. Thanks!
P.s. I am building my app for the iPhone 6s and 6s Plus.
Here is a picture of the issue, notice how the picture of Hillary Clinton looks weird.

Maybe just set the .ScaleAspectFill property for your image could works.
You can do it in the Storyboard (Attributes inspector, View -> Mode) or by adding the following line :
imgView.contentMode = .ScaleAspectFill
Hope that help you.

Related

Xamarin iOS - loading a button image from code

I have the following Image Set defined in my Media.xcassets library...
I am loading this image to a button as follows...
public override void ViewDidLoad()
{
base.ViewDidLoad();
FSDirectButton.SetImage(UIImage.FromBundle("FSDirectButton"), UIControlState.Normal);
}
However, the button is not the correct size. How do I load an image to a button and then have the button resize to fit the image? I am not sure which image is being picked (1x, 2x, 3x).
Here is a recipe that I have used in the past that has helped greatly: https://developer.xamarin.com/recipes/ios/standard_controls/buttons/use_an_image_for_a_button/
a few things you may also want to consider trying:
1. Make sure constraints are set so that the button is always the right size
2. If all else fails try adding the images to your resource folder and accessing them as files instead as a temporary workaround.
3. also, you may want to try SetBackgroundImage(), instead of SetImage to see if it yields different results.
4. Try setting the image for all the control states of the button (highlighted, disabled, etc.)
5. try changing the content mode for the imageview inside the button. It may just be that the image is too large and doesn't scale to fit the alloted space. access this by using FSDirectButton.ImageView.ContentMode
To answer your question about which image size is selected (1x, 2x, etc.), I believe that is determined by the resolution of the IOS device that the image is being displayed on. that way if someone uses an iphone or an iPad the image will still look nice and clear without you having to write a lot more code to choose the best image for each device.
How do I load an image to a button and then have the button resize to
fit the image?
If you want your button auto resizing to fit its image. Please use Autolayout to layout your button.
Try to add the top constraint and left constraint to define the element's position in X and Y. Then the button will auto adjust its size depending on its content if we don't add the width and height constraint to the button.
I am not sure which image is being picked (1x, 2x, 3x).
After adding the images, the system will decide which image should be picked depending on which device the app run on. we just need to use the image name without the suffix. More details about which device picks which type of image you can refer to this

How to show Image in full screen?

I’m trying to make simple meme app using Swift.I need to set up image at full screen.You can see Main StoryBoard with constraints here. But when im picking image that must fill full screen in portrait as result there is blank spaces in top and bottom. Similarly with landscape image but now spaces are on right and left. Anyone know how to fix this?
Set your UIImageView's contentMode to aspectFill.
Either in the storyboard…
or in code…
imageView.contentMode = .aspectFill
You can also implement a very handy third party library, easy to use : https://github.com/JanGorman/Agrume
It enables you to handle every kind of gestures, interactions, zooms etc.. on your UIImageView.
Thus, you won't have to worry about portrait/landscape constraints.
#Ashley Mills answer is the answer, mine is just an advice.

iOS 9 UIImageView scaling is erratic

I have a UICollectionView in my app. Each cell is about a third the width of the screen (in portrait orientation) and is square. Inside each cell is a UIImageView whose mode is set to "Scale To Fill" (in IB) and has constraints on each edge that force it to fill the cell.
On iOS 7 and 8, images would consistently perfectly fill each cell.
On iOS 9, scaling varies each time the view appears, and sometimes changes when the cell is clicked:
images sometimes are small, and sit in the upper left hand corner
images sometimes scale to fill as they should.
images sometimes scale slightly larger than the cell, and change size when the cell is selected.
images sometimes disappear completely when a cell is selected.
If it somehow matters, the image gets set in the cellForItemAtIndexPath method of the collection view like this:
cell.artImageView.image = artImage;
Where "artImage" is a UIImage. (Obj-C).
UIImageViews are pretty simple and straightforward. Did Apple "improve" them with some new property I need to set? Really stuck here.
Update: Tried to reproduce this in a new project and couldn't. Some kind of weird leftover IB juju. But totally replacing the collection view controller, cell and image view in IB did not fix the problem.
It turns out there was an extra view that wasn't "installed" for the size class.
I had this:
And changed it to this:
And now everything looks right!

Problems with scaling interface from a device to another in iOS

This issue is really frustrating and getting me crazy.
I've read everything about AutoLayout and so on, but it happens all the time.
My interface has a view with an image view as background and a smaller subview on top of it (corresponding to a field in my image). I've tried everything but it looks like the subview does not scale accordingly with my image when switching through devices.
By the way, the IB preview does not show what i real get when running the iOS Simulator, is that normal?
Why? What do i do wrong? I'm not sure what else to try!
http://i58.tinypic.com/34xqtc6.png
EDIT: i cannot post images :(
I have setup a githubrepo regarding your case, kindly have a look
https://github.com/usamaiqbal83/TestingProject
Constraints for Background Image View:
Constraints for Front View
the most important thing to remember is ofcourse the constraint equation
FirstItem.Attribute1 = (SecondItem.Attribute2 * Multiplier) + Constant
Instead of subview use Container View.

How to avoid distort and stretch image in Custom UIButton while using vector image set?

Below is my storyboard, I had quite a lot of constrains to make those four buttons fit into the screen nicely. However, after I put the images into the buttons and check the storyboard preview, all images are distorted. I try to adjust (had tried all of the options) the view mode at attribute inspector panel but seems no effect at all.
One more thing, I am using PDF as vector type image set, not regular .png file I am wonder if that matters? Should I change the way vector image displayed inside UIButton programaticly?
The verctor image size was 500px*500px. I resize the image into 50px*50px after #orkenstein 's reminader, xcode may not be able to handle oversized vector image that well. The image appear inside button as it is exactly 50px*50px and lost its ability to scale. Also as he sugested I should but the vector image inside an UIImageView and after my test, it works perfectly fine in an UIImageView.
Setting the contentMode on the button itself doesn't work for me, but setting it on the button's imageView does. From this Stackoverflow answer:
button.imageView?.contentMode = .scaleAspectFit

Resources