我在UITableViewCell(Swift 4)中使用ImageScrollView可可pod v1.5。 我正在使用SDWebImage从Firestore下载图像(但也试过Kingfisher,我的问题没有变化)。我的图像是700x700,并且正在ImageScrollView中显示,根据设备,它大约为400x100。我已将SDWebImage imageContentMode 设置为。 widthFill 。我旋转图像以获得我想要的桌面视图。我在其他地方使用它的常规方向。 第一次显示我的细胞时,图像显示正确。如果我回到上一页,然后再次在表格中显示相同的结果,可见单元格的图像将不再适合宽度,它们现在太宽。如果我向下滚动隐藏这些单元格,所有新单元格都会正确显示,当我向上滚动时,不正确的单元格现在正确显示。发生在模拟器和实际手机中。
以下是我的UITableViewCell类的重要部分:
class MyTableCell: UITableViewCell {
var ski : Ski!
var skiImageView: UIImageView = UIImageView()
@IBOutlet weak var skiImageScrollView: ImageScrollView!
func configureCell(ski: Ski) {
self.ski = ski
let imageUrl = ski.imageUrl!
let url = URL(string: imageUrl)
skiImageView.sd_setImage(with: url, placeholderImage: placeholderImage, options: [.retryFailed, .continueInBackground]
, completed: {
(image, error, cacheType, url) in
if (error != nil) {
print("ConfigureCell Error : \(error!)")
return;
}
let rotatedImage = self.imageRotatedByDegrees(oldImage: image!, deg: 90.0)
self.skiImageScrollView.display(image: rotatedImage) var image = self.skiImageView.image!
self.skiImageScrollView.imageContentMode = .widthFill
})
}
func imageRotatedByDegrees(oldImage: UIImage, deg degrees: CGFloat) -> UIImage {
//Calculate the size of the rotated view's containing box for our drawing space
let rotatedViewBox: UIView = UIView(frame: CGRect(x: 0, y: 0, width: oldImage.size.width, height: oldImage.size.height))
let t: CGAffineTransform = CGAffineTransform(rotationAngle: degrees * CGFloat.pi / 180)
rotatedViewBox.transform = t
let rotatedSize: CGSize = rotatedViewBox.frame.size
//Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize)
let bitmap: CGContext = UIGraphicsGetCurrentContext()!
//Move the origin to the middle of the image so we will rotate and scale around the center.
bitmap.translateBy(x: rotatedSize.width / 2, y: rotatedSize.height / 2)
//Rotate the image context
bitmap.rotate(by: (degrees * CGFloat.pi / 180))
//Now, draw the rotated/scaled image into the context
bitmap.scaleBy(x: 1.0, y: -1.0)
bitmap.draw(oldImage.cgImage!, in: CGRect(x: -oldImage.size.width / 2, y: -oldImage.size.height / 2, width: oldImage.size.width, height: oldImage.size.height))
let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return newImage
}
另一个奇怪的事情是,如果我拍打手机背面或将其放在我的桌面上,图像会跳下来,因此在ImageScrollView中只能看到我图像顶部的部分内容,但ImageScrollView用于缩放的回调不是激活,所以不知道那里发生了什么。
答案 0 :(得分:0)
您可以尝试以下方法:
imageScrollView.imageContentMode = .aspectFit
前
self.skiImageScrollView.display(image: rotatedImage)