I am using svgkit, I am able to show svg image in the cell of UICollectionView via adding the svg image to SubView, but it gives me a buggy output, when I scroll up or down the images are getting added to cell making it plastered in the cells, and it also shows the wrong svg in every cell whenever I scroll up or down the UICollectionView, is there a way I could fix that bug? Thanks!
It takes some time for SVGKit to draw your desired image.
If you scroll quickly the image drawn may no longer correspond as the cell may got reused for a different indexPath.
The solution is the same as when you load remote images (https://stackoverflow.com/a/37784212/1049134) which is also isn't instantaneous.
Related
I have a UICollectionView that is filled with images, videos, and sound bytes. Everything is working great! I am however, concerned about bandwidth usage.
I am uncertain exactly how UICollectionView with dequeued-reusable cells work, in regards to when these items will be downloaded from storage.
Let's say I have this simple cell.
selfieCell.imageView.sd_setImage(with: URL(string: message.stringOrKey), completed: nil)
return selfieCell
For purposes of answering my question, lets say I have 100 of them in a collection view cell, and only 3 cells are shown at a time.
Do all 100 images load when UICollectionView is loaded?
Or does the UICollection view wait until the cell is about to be shown to set the image (and thus download the file?)
Thank you for your help.
Only 3 cells are shown at a time
The loaded cells are only the visible ones when you scroll cellForItemAt is called and other cell is deququed from the ones who will hide at top same when you scroll up again
Also caching image like sdwebimage won't load it again from server when the relevant cell is shown again
I'm trying to display an UIImageView in a UICollectionViewCell that is loaded after the cell is displayed initially.
To not display a white are I use a fallback image. This has a different size than the image to displayed later.
The sizeForItemAtIndexPath thus is called at a time when the bigger fallback picture is displayed and returns a size that is too big for the eventually loaded image.
I've implemented a delegate to call a method after the image has finished loading. The delegate method then calls reloadItemsAtIndexPaths on the UICollectionView with the indexPath of the cell that finished loading. This happens only if the calculated height of the cell now differs from the value calculated before. The image is also stored in cache after the initial load. Together this ensures that the reloadItemsAtIndexPaths method is only called once.
Although this works in general I'm not sure if it's the best practice. It also doesn't have the best performance and with small network speeds my layout doesn't look very good.
Has anyone ever encountered the same problem or was trying to dynamically resize single cells in the UICollectionView?
I had a similar problem with an image viewing app. I used invalidateLayout on the collectionViewLayout property of the collection view. It doesn't reload the cell, but calls sizeForItemAtIndexPath for the visible cells.
I'm currently working on a project where I need to display a Facebook like feed with (depending on uploaded or not) a imageview and multiline label.
I already created a custom UITableViewCell, and the text content is showing correctly and changing the height of the cell with autolayout. But when a image is loaded into the imageview (with dynamic height), the tableview starts to jump, probably because the cell sizes are changing. I'm using SDWebImage to load the images.
Is there any way to fix this issue?
SOLUTION:
What we did is pre-load the images. Then build the tableview and cells.
Below my custom cell layout in the interface builder, the small stroke on the bottom is the dynamic image:
check this. They have explained everything in detail. Hope this helps
I am trying to have the content in a uicollectionviewcell grow bigger when it scroll moving in one direction. i initially set the size of the cell to the largest size i want, then the content inside the cell is half in terms of the size. the content is an uiimageview.
So I have a custom UICollectionViewFlowLayout in which I subclassed the layoutAttributesForElementsInRect to make the uiimageview in the cells grow bigger when it is moving in one direction. i also have shouldInvalidateLayoutForBoundsChange to return YES
Everything works perfectly when i scroll, the imageview will grow larger. However if i select a cell to push to a new viewcontroller, and then when i click the back button to come back to this uicolletionview, the enlarged uiimageview in severals cells are showing in the original size. They can only go back to the correct enlarged size if i scroll again, why is that? and how can i make it keep its enlarged size after getting pushed?
i tried invalidatelayout but it will just refresh the layoutattributes with correct enlarged size of the content, but the view is just not updated eventhough the size is already enlarged.
thanks
I had a similar issue when designing cv cells and would gess the issue is with when the subviews get calls to update their size, which might not happen when the view refreshes.
Instead of resizing content in the cell, you might want to dynamically size the cells themselves and have the content automatically adjust to that size. Calling [mycollection invalidateLayout] then should be all you need, maybe not even that.
The method is
collectionView:layout:sizeForItemAtIndexPath:
which you declare in your UICollectionViewFlowLayout.h
If you then run into trouble where subviews do not resize with cell size, related problems can arise from constraints/autolayout etc of the subviews.
I have a UITableView which loads the icons for my other apps.
They look fine at a good 57x57 automatic size in a cell which is 70px height.
When I click on a cell, the image suddenly scales up to 70x70 and I have no idea how to stop it.
Any ideas?
Thanks,
Ashley
So the way I understand it is you are adding a UIImageView as a subview to a cellview.
The way I do this that works great even where the image is smaller than the cells row hieght is add the UIImageView in cellForRowAtIndexPath then set the frame to something that fits in the cell and is the image size.
You might be building the custom cells in the UI Builder, but explicitly setting the frame of the image view to CGRectMake(imageview.frame.bounds.x,imageview.frame.bounds.y,57,57) and setting the autoresize property for vertical and horizontal to none should stop it from scaling up.