UIImageView显示不正确

时间:2018-01-01 20:58:27

标签: ios swift uitableview uiimageview

我已将UIImageView放置在我的自定义单元格中(Swift 3):

let cell = self.tableView.dequeueReusableCell(withIdentifier: "Feedcell", for: indexPath) as! FruitTableViewCell

let hero: List_Feed
        hero = heroes[indexPath.row]

if(hero.photo==""){
            cell.cnHeight.constant = 0
            cell.cnTopMargin.constant = 0
            cell.cnBottomMargin.constant = 52
            cell.imgPhoto.image = UIImage()
            cell.imgPhoto.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
        }else{
            cell.cnTopMargin.constant = 14
            cell.cnBottomMargin.constant = 50
            cell.imgPhoto.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
            //displaying image
            Alamofire.request(hero.photo!).responseImage { response in
                debugPrint(response)
                if let image = response.result.value {
                    //let newWidth = containerView.frame.height * ratio
                    let bounds = UIScreen.main.bounds
                    let width = bounds.size.width
                    let imagewidth = image.size.width
                    let imageheight = image.size.height
                    let ot1 = width / imagewidth
                    let ot2 = round(ot1 * imageheight)
                    //let ot3 = round(ot2 / 2)
                    //let ot4 = ot3 - 12
                    //let ot5 = ot4 + 50
                    cell.cnHeight.constant = ot2
                    cell.imgPhoto.image = image
                }
            }
        }

应该如何显示: enter image description here

当我向下滚动然后向上滚动时,它是如何显示的:

enter image description here (突然uiimageview涵盖所有自定义单元格空间)

1 个答案:

答案 0 :(得分:0)

尝试添加

DispatchQueue.main.async {
    // all your ui code
}

你可能还在后台线程上。