I am loading the images from server using SDWEBIMAGE library downloaded from github.
https://github.com/rs/SDWebImage
This is the view to my prototype cell.
Image width and height is set to static and View-> Mode -> Scale to Fill.
Now when i load images from server it does layout issues, image is not placed properly in image container and not even scale to fill, Overlapping every thing tried to find a solution but nothing work's:
Another Screenshot
Tried allot to solve the issue but nothing found, if possible the image should resize it self but not to change the layout constrain's.
Here is my code:
[cell.imageView sd_setImageWithURL:urlimage placeholderImage:[UIImage imageNamed:#"service-1.png"]];
[cell.imageView setContentMode:UIViewContentModeScaleToFill];
Thanks in advance.
Since you are loading the image async, constraints will be set before image is ready, therefore it will use the UIImageView's edges for constraints. But because of content mode UIViewContentModeScaleToFill, edges are not matching with image size at the run time until the download is complete. You can make loading sync but it's not a good practice since it will block the UI. You have to come up with different workaround.
You can post a notification when image loading is completed and set the constraints with received notification. Therefore view will use latest layout constraints.
Related
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
#nathan this is my entire controller.
My problem is that when I click a tableview cell, the image size is changed and becomes too long.
I can not figure out how to fix the size, I tried everything.
To do URL parsing, I used NSXMLParser, while I used SDWebImage to load images into cells.
When I press an element its size changes.
Like this:
How to fix?
That seems like a content mode issue. Set the imageView's contentMode to aspectFit/aspectFill (depends on what you want to achieve)
I'm using a framework to asynchronously load images (Pinterest's PINRemoteImage) in combination with a UITableView. On one of my UITableViewCells I have such an image and I first initialize it with a placeholder and it's then later replaced with the real image. I don't know the real image's dimensions at time of loading and therefore the placeholder's dimensions and the ones of the real image may be different.
Using KVO, I watch the UIImageView's image property and when it's re-set to the real image, I adjust the the view's height constraint to the appropriate size. This directly shows when I first see the image. It has the appropriate size.
The cell's contentView however, does not update it's height to fit around the image (and the other views) properly until the cell get's dequeued a second time. Only then is has the cell the appropriate height.
I assume that I have to tell the table or the cell itself that it needs to update the layout manually. But between all the layout… functions I'm not quite sure which to use where right now.
So, where's the best way to make the cell update it's height to the updated image height? Other answers to similar questions on SO suggest to reload the cell, but that seems a bit too much to me. And I'd have to build a callback from the cell (where the image lives) to the table itself which could then trigger the reload.
Thanks!
I have added a table view, and I am display image in the cells. I have also added this code:
So that the cells resize depending on the image.
When I launch my app though, I get this :
[![enter image description here][1]]
And the images do not load untill I start scrolling...If I scroll down half the page then go back to the top, I get this: Which is correct
[![enter image description here][2]][2]
Any ideas? I have researched on google and tried the odd solution for the older versions of Xcode, But nothing seems to work!
If the image is already downloaded and resident in your cache when you pass the post to the cell them the image view will have an image and therefore an intrinsic content size. This all allows the table view to calculate the required layout.
If the image needs to be downloaded then the image view won't have an image and will size to zero because there is no intrinsic content size other than that.
If your images are all the same size then you can supply a placeholder image while waiting for the download and the cell will size appropriately.
Note also that you aren't updating the cell when the image has downloaded. When you do this you do need to check that the cell hasn't been reused (or you'll be showing the wrong image). If you don't have a placeholder or can't guarantee the image size then you will also need to ask the table view to update.
Maybe your post.downloadImage() and post.fetchLikes() should callback, and in callback, should reload cell in mainThread. hope it help.
I'm trying to address a slow scrolling and slow loading UIViewController that uses a UITableView with custom background cells.
When I don't set the backgroundView of the cell with an image, the performance is great. So I'm wondering if it's more performant to supply the backgroundView with images that are the actual size of my tableview row or is it better to use smaller images that are resized to fit the row's dimensions?
The images are all local and already included in the app - there's no remote fetching involved.
Improving load time of UITableView that has cells with custom background images
Try to look to use an NSOperationQueue to handle lazy loading of images and a custom tableviewcell.
Google for tweetie custom tableviewcell That should set you in the right direction.
Apple has a sample project for downloading images in tableViews: LazyTableImages