我有一个UIImageView,大小是(宽=屏幕大小,高=239)。我现在下载了一个大小为(宽=1366,高=445)的图像。
我不能使用"纵横比匹配"(因为白色背景)或"缩放"(调整纵横比)。我确实想使用方面填充并以一种特定的方式对齐它,即在右对齐的方面填充或在左对齐的方面填补。有人知道怎么做吗?
我使用过像Toucan这样的不同库,但我似乎无法让它发挥作用。谢谢大家。
您可以使用SDWebImage或Kingfisher来显示占位符图像并进行缓存。。。然后在它的完成块中写一个代码来调整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!)
}
}
}
希望这能有所帮助。