简体   繁体   中英

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. Regardless of the values in CGRect , the 'icons' remain the same size.

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

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 . Let me know, if you face any problem with Custom Tableview Cell.

Custom tableview cell, gives flexibility to arrange all cell elements according to your need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM