简体   繁体   English

使用imageView调整图标大小

[英]Resizing icon using imageView

I've got the following code (updated): 我有以下代码(已更新):

    // Set cell row height
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{

    return UITableViewAutomaticDimension  // Auto-size cell based on content

}

// Set cell content
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

    // Configure the cell...
    let weatherObject = forecastData[indexPath.section]

    // Convert UNIX time into readable format
    let sunriseUNIX = weatherObject.sunriseTime
    let sunriseDate = Date(timeIntervalSince1970: Double(sunriseUNIX))

    let sunsetUNIX = weatherObject.sunsetTime
    let sunsetDate = Date(timeIntervalSince1970: Double(sunsetUNIX))

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "h:mm a"
    let sunriseConv = dateFormatter.string(from: sunriseDate)
    let sunsetConv = dateFormatter.string(from: sunsetDate)


    cell.textLabel?.text = weatherObject.summary
    cell.detailTextLabel?.text = "Max: \(Int(round(weatherObject.temperatureMax))) °F / Min: \(Int(round(weatherObject.temperatureMin))) °F \nWill feel like \(Int(round(weatherObject.apparentTemperatureMax))) °F \nSunrise: \(sunriseConv)   Sunset: \(sunsetConv) \nRelative Humidity: \(Int((weatherObject.humidity)*100))% \nMoon Phase: \(round(weatherObject.moonPhase))"
    cell.detailTextLabel!.numberOfLines = 0
    cell.imageView?.image = UIImage(named: weatherObject.icon)
    cell.imageView?.frame = CGRect(x: 0, y: 0, width: 10, height: 10)
    cell.imageView?.contentMode = .scaleAspectFit
    cell.imageView?.clipsToBounds = true  // clipping will help check actual frame of image
    cell.layoutIfNeeded()
    cell.setNeedsDisplay()  // try to reset display, if not updating your image scale automatically

    return cell
}

...(Still) yielding the following result: ...(仍然)产生以下结果:

代码结果

I'm trying to get the 'icon' images sized 10 x 10 pixels, yet the above code doesn't work. 我正在尝试获取大小为10 x 10像素的“图标”图像,但是上面的代码不起作用。 Regardless of the values in CGRect , the 'icons' remain the same size. 无论CGRect中的值CGRect ,“图标”的大小均保持不变。

Any pointers, alternatives would be appreciated!! 任何指针,替代品将不胜感激!

Thanks in advance. 提前致谢。

您应正确设置图像的内容模式,以将其缩放到正确的尺寸。

cell.imageView.contentMode = .scaleAspectFit

You can use code as shown under: 您可以使用如下所示的代码:

var newImgThumb : UIImageView
newImgThumb = UIImageView(frame:CGRectMake(0, 0, 100, 70))
newImgThumb.contentMode = .ScaleAspectFit 

Hope u got it!!! 希望你明白了!

Set content mode of cell image view as scaleAspectFit 将单元格图像视图的内容模式设置为scaleAspectFit

cell.imageView?.image = UIImage(named: weatherObject.icon)
cell.imageView?.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
cell.imageView?.contentMode = .scaleAspectFit
cell.imageView?.clipsToBounds = true  // clipping will help check actual frame of image
cell.layoutIfNeeded()
//cell.setNeedsDisplay()  // try to reset display, if not updating your image scale automatically

A good advice for you, Use Custom tableview cell and set all elements according to your choice/requirement. 一个不错的建议, 使用“自定义表格”单元格并根据您的选择/要求设置所有元素。

Here is reference tutorial for Custom Tableview cell . 这是自定义Tableview单元的参考教程 Let me know, if you face any problem with Custom Tableview Cell. 让我知道,如果您遇到自定义Tableview单元的任何问题。

Custom tableview cell, gives flexibility to arrange all cell elements according to your need. 自定义tableview单元,可根据您的需要灵活地安排所有单元元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将ImageView调整为UIViewContentModeScaleAspectFit内容的大小 - Resizing ImageView as the UIViewContentModeScaleAspectFit content 基于使用SDWebImage(AUTOLAYOUT)下载的图像高度更改imageView高度约束后,TableView单元格未调整大小 - TableView cell not resizing after changing imageView height constraint based on Image height downloaded using SDWebImage (AUTOLAYOUT) 调整imageview iOS大小时的坐标不同 - Different coordinates while resizing an imageview iOS 调整大小时,UIButton的imageView变为隐藏 - UIButton's imageView becomes unhidden when resizing 调整UIImage的大小以适合表格单元格ImageView - Resizing UIImage to fit table cell ImageView 防止UIButton.imageView按图像调整大小 - Prevent UIButton.imageView from resizing by image 天气 pp 中的 imageview 中未显示天气图标 - weather icon not displaying in imageview in weather pp TableCell ImageView设置带有URL的图像:SDWebImage:仅调整第一行的大小 - TableCell ImageView Set Image With URL: SDWebImage: Only resizing first row 调整UIBarButtonItem的Imageview大小以使图像按比例缩小(iOS) - Resizing UIBarButtonItem's Imageview so that the Image is Scaled Down (iOS) 使用AutoLayout调整UIStackView的大小 - Resizing a UIStackView using AutoLayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM