ios - 表格单元格 View 中的图像在运行时没有适当的大小,并且在我滚动时自行更改大小和剪裁边界

标签 ios swift view

当我在 模拟器 上运行我的应用程序时,我通过 http 请求加载的图像没有适当的大小和边界剪辑。我希望它们的大小为 60x60 并为圆形,但它们会随机缩放以适合 UITableViewCell 但在我上下滚动后它们保持固定但仍然很大,我不知道是什么原因造成的,我也不知道如何解决它,我是 iOS 的新手。我将在 Table Cell 中发布带有我的 UIImageView 的屏幕截图以及我第一次运行应用程序和我的 View Controller 类时的效果。

我试图破坏约束,在 UIImageView 上设置固定的宽度和高度约束,但没有结果。 我还尝试从单元格 View 中禁用 subview 自动调整大小,但没有结果。

这是效果,这是在我开始滚动之前发生的:

enter image description here

这发生在我上下滚动后,边界上的裁剪恢复正常但尺寸仍然很大:

enter image description here

这是我的带有单元格 ImageView 的 Storyboard:

enter image description here

这是我的 ViewController.swift 类:

//
//  ViewController.swift
//  TopDevelopers
//
//  Created by Eduard Valentin on 12/04/2018.
//  Copyright © 2018 Eduard Valentin. All rights reserved.
//

import UIKit
import Alamofire
import Foundation

struct UserInfo {
    var name:String
    var imageURL:String
    var imageView:UIImageView

    init(newName: String, newImageURL:String, newImageView: UIImageView) {
    self.name = newName
    self.imageURL = newImageURL
    self.imageView = newImageView

    }
}

class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource{

@IBOutlet weak var tableView: UITableView!

var users:[UserInfo] = []



override func viewDidLoad() {

    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self

    // GET the data from the stackexchange api

    let param: Parameters = [
        "order": "desc",
        "max" : 10,
        "sort" : "reputation",
        "site" : "stackoverflow"
    ]

    Alamofire.request("https://api.stackexchange.com/2.2/users", method: .get, parameters: param).responseJSON { (response) -> (Void) in


        if let json = response.result.value {
            // we got a result

            /* I know this is a bit ugly */
            let json1 = json as! [String:AnyObject]
            let usersInfoFromJSON = json1["items"] as! NSArray       // remember to cast it as NSDictionary


            for userInfo in usersInfoFromJSON {

                let userDict = userInfo as! NSDictionary

                Alamofire.request(userDict["profile_image"] as! String).responseData { (response) in
                    if response.error == nil {
                        print(response.result)
                        // Show the downloaded image:
                        if let data = response.data {
                            let imageView = UIImageView()
                            imageView.image = UIImage(data: data)
                            self.users.append(UserInfo(newName: userDict["display_name"] as! String,
                                                       newImageURL: userDict["profile_image"] as! String,newImageView: imageView))
                            self.tableView.reloadData()
                        }
                    }
                }
            }
        }
    }

}


@available(iOS 2.0, *)
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.users.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

@available(iOS 2.0, *)
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! CustomTableViewCell
    cell.cellImageView.image = self.users[indexPath.row].imageView.image
    cell.cellImageView.layer.cornerRadius = (cell.cellImageView.layer.frame.height / 2)
    cell.cellLabel.text = self.users[indexPath.row].name

    return cell
}

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

编辑 1: 我还尝试将内容模式设置为“缩放以适合”、“宽高比”仍然是相同的结果。

编辑 2: 好的,我通过删除几乎所有内容并重新执行来解决它,但这次我确实为“建议的约束”设置了选项,我也使用 xib 作为现在细胞和一切正常,我仍然不知道是什么原因造成的。

最佳答案

设置clipsToBounds属性为true,并根据cellImageView的frame而不是cellImageView.layer的frame设置frame:

cell.cellImageView.clipsToBounds = true 
cell.cellImageView.layer.masksToBounds = true
cell.cellImageView.contentMode = .scaleAspectFit
cell.cellImageView.layer.cornerRadius = cell.cellImageView.frame.height / 2

并尝试在您的结构中添加 UIImage 而不是 UIImageView。并在 cellForRowAtIndexPath 中用作:

cell.cellImageView.image = self.users[indexPath.row].image

关于ios - 表格单元格 View 中的图像在运行时没有适当的大小,并且在我滚动时自行更改大小和剪裁边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49813810/

相关文章:

ios - 单元格一开始不出现,但如果我向下滚动并返回,则会显示

php - 如何修改与auth相关的flash消息的外观?

iphone - iPhone上图片的路径

objective-c - 在类型 'title' 的对象上找不到属性 'id'

swift - 调用 UIActivityViewController 时 IOS 8 iPad 应用程序崩溃

android - 不使用 invalidate(Rect) 只更新 View 的一部分

ios - 如何解决ios中个人热点开启时 View 中断的问题?

android - 移动应用程序的图像大小最佳实践

ios - 折扣与购买的商品数量成正比

ios - WKWebView 是否使用来自 Safari 的 cookie?