简体   繁体   中英

dynamic image resizing after asynchronously downloading it from the firebase

I am trying to dynamically adjust the image size depending upon the image that I download from the firebase database and present it in the table view cell. I want the width of the image to be the frame width and the height to adjust accordingly.

I have a customImageView class to download image asynchronously from firebase

I've tried many methods but in vain. Is there a super clean way to adjust the image size dynamically :-D

this Is my cell :

class viewControllerHomeCell: UITableViewCell  { 

   @IBOutlet var mainImage: CstmImageView!

  }

my cell for row at :

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellData =  TableViewControllerHome.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! viewControllerHomeCell

    cellData.mainImage.loadImagesWithUrl(from: self.postArray[indexPath.row].covrImgUrl)
  }

and this is a UIImageView subclass to load images from the firebase async. :

class CstmImageView : UIImageView {
let imageCache = NSCache<AnyObject, AnyObject>()

var imageUrlString : String?
func loadImagesWithUrl(from imageUrl : String!)  {
    imageUrlString = imageUrl
    self.image = nil

    if let cachedImage = imageCache.object(forKey: imageUrl as AnyObject) as? UIImage {
        self.image = cachedImage
        return
    }

    let url = URLRequest(url: URL(string: imageUrl)!)
    URLSession.shared.dataTask(with: url) { (data, response, eror) in

        if eror != nil {
            print(eror!)
            return
        }

            DispatchQueue.main.async {
            if let downloadedImage = UIImage(data : data!) {
                if self.imageUrlString == imageUrl {
                  self.image = downloadedImage
                }
                self.imageCache.setObject(downloadedImage, forKey: imageUrl as AnyObject)
            }

        }
    }.resume()
 }
}

any help is appreciated aa lot : )

Take your imageView Height Outlet and connect with your imageView height constrain

class viewControllerHomeCell: UITableViewCell  { 

    @IBOutlet var mainImage: UIImageView!
    @IBOutlet var viewImageViewHeight: NSLayoutConstraint!
 }

And Use Pod for Download Image

pod 'SDWebImage'

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
 {
     let cellData =  TableViewControllerHome.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! viewControllerHomeCell

        cellData.mainImage.sd_setImage(with:URL.init(string: "Pass Yor image Url string here"))
        { (image, error, imageCacheType, url) in

             cellData.mainImage.image = image

             viewImageViewHeight.constant =  image?.size.height
        }

        cellData.mainImage.loadImagesWithUrl(from: self.postArray[indexPath.row].covrImgUrl)
}

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