Dynamic UIImageView Size within UITableview [動態調整Imageview圖片高度]

July 25, 2017 0 Comments

OK, This is one of my code test problem when I interview iOS developer. In that time I didn't answer very well and I didn't get that job. But after I reorganized it, it's not so difficult to do it.

So, support we went download image from url and show it on UITableView, and we need to dynamic the ImageView in case some images are portrait, and some are landscape.

If we not doing anything, then they will look like:


As we can see first one is landscape image, but second one is portrait. And second one's image is squeezed by its frame. So we need to do some change when image is portrait.


Here is way we do:

1. Download image from internet url (we use SDWebImage sdk) and show on tableview
2. In  tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) we calculated the images ratio.
3. If is portrait image, then we update this row.


Here is demo code you can download: github

*In this project, you also can see I use custom class to save json data.

0 comments: