Swift 4 ImageScrollView 在 UITableView 中调整可见单元格的大小



我在UITableViewCell(Swift 4(中使用ImageScrollView cocoa pod v1.5。 我正在使用SDWebImage从Firestore下载图像(但也尝试了翠鸟,但我的问题没有变化(。我的图像是 700x700,并且显示在 ImageScrollView 中,具体取决于设备,大约为 400x100。我已将 SDWebImageimageContentMode设置为 。宽度填充。我旋转图像以使其以我想要的表格视图形式。我在其他地方的常规方向使用它。 第一次显示我的单元格时,图像会正确显示。如果我回到上一页,然后再次在表格中显示相同的结果,可见单元格的图像将不再正确适合宽度,它们现在太宽了。如果我向下滚动隐藏这些单元格,则所有新单元格都正确显示,当我向上滚动时,不正确的单元格现在正确显示。发生在模拟器和实际手机中。

以下是我的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的用于缩放的回调没有激活,所以也不知道那里发生了什么。

您可以尝试以下操作:

imageScrollView.imageContentMode = .aspectFit

以前

self.skiImageScrollView.display(image: rotatedImage)

最新更新