UIImageView Resizing / Scaling - SWIFT

烂漫一生 提交于 2019-12-06 05:46:05

You can use SDWebImage or Kingfisher to show placeholder image and for caching... Then in its completion block write a code to resize the imageView

@IBOutlet weak var yourImageView: UIImageView!

func loadImage(){

 SDWebImageManager.sharedManager().downloadImageWithURL(imgURL, options: SDWebImageOptions.CacheMemoryOnly, progress: { (received, expected) -> Void in

    }) { (theImage : UIImage!, error : NSError!, cacheType : SDImageCacheType!, bool : Bool, imageUrl : NSURL!) -> Void in

         let aspect = theImage.size.width / theImage.size.height

         self.aspectConstraint = NSLayoutConstraint(item: self.yourImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.yourImageView, attribute: NSLayoutAttribute.Height, multiplier: aspect, constant: 1.2)

         self.yourImageView.image = theImage
    }
}//loadImage


var aspectConstraint: NSLayoutConstraint? {

    willSet {

        if((aspectConstraint) != nil) {
            self.yourImageView.removeConstraint(self.aspectConstraint!)   
        }
    }

    didSet {

        if(aspectConstraint != nil) {
            self.yourImageView.addConstraint(self.aspectConstraint!)
        }

    }

}

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!