簡體   English   中英

圖像不適合圖像視圖

[英]image does not fit in the image view

我在swift做一個項目有一個圖像視圖,當我在圖像視圖中設置圖像時,我從圖庫中獲取圖像,圖像與圖像視圖的寬度和高度不匹配。 超時圖像寬度和高度小於圖像視圖這里是代碼:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "ImageTVCell"
        index=indexPath.row
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? ImageTVCell
       // cell?.imageView?.clipsToBounds=true
        cell?.imageView?.contentMode = UIViewContentMode.scaleAspectFit
        cell?.imageView?.image=imageArray[indexPath.row]
        return cell!
    }

使用.aspectfill並將clipsToBounds設置為true

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "ImageTVCell"
        index=indexPath.row
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? ImageTVCell
        cell?.imageView?.clipsToBounds=true
        cell?.imageView?.contentMode = UIViewContentMode.scaleAspectFill
        cell?.imageView?.image=imageArray[indexPath.row]
        return cell!
    }
cell?.imageView?.contentMode = UIViewContentMode.scaleAspectFit 

您的代碼中的圖像大小適合您的imageview。 你應該用,

cell?.imageView?.contentMode = UIViewContentMode.scaleAspectFill

還使clipsToBounds = true。

要更好地了解Imageview內容模式,請訪問http://blogs.innovationm.com/image-handling-in-ios/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM