如何调整图像大小以适应 tableview 单元格中的 UIImageview
how to resize the image to fit in UIImageview in a tableview cell
我正在从用户那里获取标题、描述和产品照片等数据,并将其显示在帖子页面上。我得到图像,将 url 下载到 firebase,然后使用 url session 再次获取它,这是我获取图像的代码:
class ImageService {
static let cache = NSCache<NSString, UIImage>()
static func downloadImage(withURL url:URL, completion: @escaping (_ image:UIImage?)->()) {
let dataTask = URLSession.shared.dataTask(with: url) { data, responseURL, error in
var downloadedImage:UIImage?
if let data = data {
downloadedImage = UIImage(data: data)
}
if downloadedImage != nil {
cache.setObject(downloadedImage!, forKey: url.absoluteString as NSString)
}
DispatchQueue.main.async {
completion(downloadedImage)
}
}
dataTask.resume()
}
static func getImage(withURL url:URL, completion: @escaping (_ image:UIImage?)->()) {
if let image = cache.object(forKey: url.absoluteString as NSString) {
completion(image)
} else {
downloadImage(withURL: url, completion: completion)
}
}
,这是我设置此数据的代码:
func set(post:Post) {
ImageService.getImage(withURL: post.imageid) { (image) in
self.postimage.image = image
}
titlel.text = post.title
usernamel.text = post.username
desct.text = post.desc
}
,当应用程序为 运行:no good, and nogood2. also, here is a photo of my cell and they all have constraints so I don't think the problem is in this cell but still 时,我的帖子看起来像这样。我只是希望用户将图像视为正方形的普通图片,而不是像这样看起来又高又瘦。
将图像视图内容模式设置为纵横比填充和剪辑以将 属性 绑定为 true。
self.postimage.contentMode = .scaleAspectFill
self.postimage.clipsToBounds = true
使用self.postimage.contentmode.scaleaspectfit
在您的屏幕截图中,我可以看到您没有提供高度限制,这就是图像很高的原因。要显示正方形图片,只需在 UIImageView
上提供宽度和高度约束
我正在从用户那里获取标题、描述和产品照片等数据,并将其显示在帖子页面上。我得到图像,将 url 下载到 firebase,然后使用 url session 再次获取它,这是我获取图像的代码:
class ImageService {
static let cache = NSCache<NSString, UIImage>()
static func downloadImage(withURL url:URL, completion: @escaping (_ image:UIImage?)->()) {
let dataTask = URLSession.shared.dataTask(with: url) { data, responseURL, error in
var downloadedImage:UIImage?
if let data = data {
downloadedImage = UIImage(data: data)
}
if downloadedImage != nil {
cache.setObject(downloadedImage!, forKey: url.absoluteString as NSString)
}
DispatchQueue.main.async {
completion(downloadedImage)
}
}
dataTask.resume()
}
static func getImage(withURL url:URL, completion: @escaping (_ image:UIImage?)->()) {
if let image = cache.object(forKey: url.absoluteString as NSString) {
completion(image)
} else {
downloadImage(withURL: url, completion: completion)
}
}
,这是我设置此数据的代码:
func set(post:Post) {
ImageService.getImage(withURL: post.imageid) { (image) in
self.postimage.image = image
}
titlel.text = post.title
usernamel.text = post.username
desct.text = post.desc
}
,当应用程序为 运行:no good, and nogood2. also, here is a photo of my cell and they all have constraints so I don't think the problem is in this cell but still 时,我的帖子看起来像这样。我只是希望用户将图像视为正方形的普通图片,而不是像这样看起来又高又瘦。
将图像视图内容模式设置为纵横比填充和剪辑以将 属性 绑定为 true。
self.postimage.contentMode = .scaleAspectFill
self.postimage.clipsToBounds = true
使用self.postimage.contentmode.scaleaspectfit
在您的屏幕截图中,我可以看到您没有提供高度限制,这就是图像很高的原因。要显示正方形图片,只需在 UIImageView
上提供宽度和高度约束