UIImageViewresize/缩放 – SWIFT

我有一个UIImageView的大小(宽度=屏幕尺寸,高度= 239)。 我现在下载一个大小(宽度= 1366和高度= 445)的图像。

我不能使用Aspect Fit(因为白色背景)或缩放(拧紧纵横比)。 我确实想使用Aspect Fill并以特定的方式对齐它 – 即右对齐的方面填充或左对齐的方面填充。 有谁知道如何做到这一点?

我使用过像Toucan这样的不同库,但我似乎无法让它工作。 谢谢一堆SO。

您可以使用SDWebImageKingfisher显示占位符图像和缓存…然后在其完成块中编写一个代码来调整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!) } } } 

希望这可以帮助。