First of all I'm trying to lazy load images from remote url and what I've done so far is as follows,
- I have created custom UITableViewCell which contains on UIImageView and other labels using XIB and mapped it to my custom UITableViewCell class.
- I'm using the custom table cell in my UITableView
- UIImageView inside the custom cell, loads remote image as per Apple's tutorial for lazy loading images for tableview
- Initially ImageView displays an loading image, once the image is loaded, the loaded image gets updated inside ImageView or if it fails, No Image will get displayed
And the problem here is, the remote images are of variable sizes, lets say if the image is smaller than the ImageView frame the image stretches and display as blurry but once we scroll or on row selection its re-size to actual size and looks fine.
At the same time if the image is larger than the ImageView frame it fits correctly fit inside the frame but again if we scroll or select the row it occupy size bigger than frame and hides other elements in the row
And I searched for hours and all I got is setting content mode to UIViewContentModeScaleToFit and setting clipsToBounds to 'YES', I made those changes as well both in XIB, as well as in Code but it doesn't change anything. Can someone point me, on what I'm doing wrong or how to fix this issue.
Thanks.
Grab the image however you want
UIImage *customImage = [UIImage imageWithContentsOfFile:theImagePath];
Then check the size of the image using:
NSLog(#"the customImage width is %f and height is %f ",customImage.size.width,customImage.size.height);
Then modify the width and height of the frame that your placing it in so that they are set to an equal fraction of whatever the original image was (while still fitting in the necessary bounds)
float newWidth = customImage.size.width *.4;
float newHeight = customImage.size.height *.4;
ImageButton.frame = CGRectMake(x coord, y coord, newWidth, newHeight);
You can also throw on aspect fit/fill if necessary
I just re-sized the image, if the image's width and height is greater than the frame size and displayed the re-sized image on it, and if the image dimension is smaller than the frame I displayed as it is.
Related
I am using the attached prototype cell layout in a tableview. Each cell is sized using autolayout:
tableView.rowHeight = UITableViewAutomaticDimension
The "tagline" is a string and sets the height of the cell depending on the length of the string. Given a greater length the height of the UIImageView also resizes. The ImageView is set to a variety to images with different sizes and aspect ratios. I would like to resize the image based on the beginning width of the ImageView(that does not change) and the resulting height and the image's aspect ratio.
The code to set the two is very straightforward.
cell.taglineLabel.text = tagline
cell.recipeImageView.image = image
Inside a tableView cellForRowAt function I set the tagline first in the code, then the Image. However, if I attempt to resize the image and I inspect the bounds or frame size, the size is the original size. I am assuming the autosize is performing after the func. Therefore, I cannot determine size.
Is there a way to force the autosize after the tagline is set, so I can see the frame/bounds sizes I have to work with?
I'm using UIImageView with Content Mode set to Aspect Fit. UIImageView resizes correctly my image but it doesn't change own frame after resizing even there is no constraints set.
What I need:
I need UIImageView to resize own frame according image inside. I've configured image view at Storyboard.
I need to layout again other objects at view because UIImageView's size is changed.
What I've tried:
I've created:
UIViewController (white background) → UIImageView (gray background) → sample image inside it.
Constraints: leading & trailing constraints set to zero, Y constraint is set to align UIImageView vertically.
As you see after image scaled correctly UIImageView's frame is still has incorrect size (gray background).
I've tried to set UIImageView size manually:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let newSize = AVMakeRect(aspectRatio: backgroundImageView.image!.size, insideRect: backgroundImageView.frame)
backgroundImageView.frame = newSize
}
In that example code I've calculated image size and set frame size to image's size. I don't know if it is better way so I don't need to use AVFoundation's methods.
Problem: other object is place incorrectly because UIImageView's size changed and constraints are invalid now.
That methods doesn't work for me:
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
Even I will call my UIImageView size calculation at viewDidLayoutSubviews() other constraints is still using old UIImageView's size.
Questions
How to change UIImageView's size correctly? Maybe I can do it without code? Maybe I need to play with Hugging/Compression? I've tried something but got no luck.
How to force view to recalculate other constraints so they will use new UIImageView size? For example I can put red square at the top of UIImageView, but after UIImageView has been resized by my method, red square still will be at the old UIImageView's position.
In this image, height of images in imageview are long. if i try to give constraint height, then the imageview with no image also gets height which i dont want since i want to collapse the tableviewcell with no image.
Its working fine with no image now but only problem is the height.
Follow below steps:
::Put a constraint of height on ImageView
::Take an outlet of constraint
::If there is no image then make constraint.constant to 0 otherwise dont change the constraint.
Images are represented as instances of UIImage class, but this class does not show the image. It works the class UIImageView which has the property image which is UIImage. UIIMageView will present(show) the image contained in it's .image property using dimensions defined in image.size property. So, if you want to change the height, you can do this using the following code:
myImageView.frame.size.height = 50 // or calculate and put here your height
Every instance of UITableViewCell already has an imageView property which is UIImageView and you can access it and control it programmatically.
I have a UIImage in a tableview cell that I want to show in a circle. When my table is rendered the first time the images are shown in a diamond shape instead of a circle, but when I go to that same screen after the first time the images are rendered in circle. I have found the problem, but I don't know the solution. Here's the problem: In the storyboard the UIImage view has a width and height set to 135, this is a random number because I assumed that I could overwrite it with the following two constraints in the storyboard: 1) a constraint that sets the height proportional to the superview's height and 2) a 1:1 ratio for the height and the width of the UIImage. Here's the snapshot of my constraints:
I added prints to tableView:cellForRowAtIndexPath where I change the layout to a circle to see the UIImage width. The first time it prints 135 and the second time it prints 100. This means that the proportional size constraints weren't effective in the first round, but they are effective after. I tried to set the fixed height and width in the storyboard to zero but that didn't help, it showed the image in a square. How can I enforce the proportional constraints all the time? Or disable the fixed size properties for this particular view? Thanks!
P.S. Here's the code inside tableView:cellForRowAtIndexPath that changes the layout to a circle (nothing else in that method that touches the image or the layout):
cell.imageView.layer.cornerRadius = cell.imageView.frame.size.width / 2
cell.imageView.clipsToBounds = true
cell.imageView.image = data.image
Try to reload table in viewWillLayoutSubViews.
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
[self.tableView reloadData];
}
I have a UIImageView with Auto-layout to the container margins. I set a UIImage to it in the ViewController. If I use UIViewContentModeScaleAspectFit then the image is centred in the middle of the screen as I wanted and everything looks great, but when I give the UIImageView a background color, I can see it still spreads all the way to the container margins, and doesn't get the image's proportions and dimensions. This is my code:
UIImage *passedImage = [UIImage imageNamed:self.photoTitle];
CGRect imageBounds = CGRectMake(0, 0, passedImage.size.width, passedImage.size.height);
[self.imageView setImage:passedImage];
self.imageView.bounds = imageBounds;
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
I have tried everything to fix it and looked everywhere for answers, please help me, you are my only hope.
Instead of pinning the image view's edges to the container margins, pin its center X and center Y to the container's center X and center Y.
This gives the same visual result — the image is centered — but leaves the image view free to resize itself according to its contents.
UIImageView does not resize itself according to image size it renders. If you need to size it accordingly, you need to do it yourself. Matt's answer is reasonable, but you still need to update your image view size at some point. I'd suggest doing it in layoutSubviews or updateConstraints method of your view or in view controller's viewDidLayoutSubviews.