在显示新图像之前,在同一个 UIImageView 中加载多个图像会闪烁最后一个图像 [英] Loading multiple images in same UIImageView flashes last image before showing new image
问题描述
目标是使用相同的 UIImageView 渲染不同的图像.每次用户点击不同的按钮时,UIImageView 中都会呈现不同的图像.
The goal is to render different images using the same UIImageView. Each time the user taps a different button, a different image gets rendered in the UIImageView.
示例用户流程:
- 用户点击图像 1 的按钮.UIImageView 显示为已加载图像 1.
- 用户点击不同的按钮来关闭 UIImageView.UIImageView 被隐藏.
- 用户点击图片 2 的不同按钮. UIImageView 加载图片 2 然后取消隐藏.图像 1 在图像 2 出现之前出现片刻.
这是第一次有效.但是,随后的每个时间都会在显示第二张图像之前显示前一图像的一瞥,从而导致用户体验闪烁.例如,在查看第二张图片时,您会在第二张图片出现在屏幕上之前看到第一张图片的短暂片刻.
This works the first time. However, each subsequent time shows a glimpse of the prior image for a split second before showing the second image, causing a flickering user experience. For example, when viewing the second image, you see the first image for a brief moment before the second one appears on screen.
如何修复后续加载,以免出现先前的图像?
How can you fix subsequent loads so the prior image doesn't appear?
此函数将新图像加载到 UIImageView 中:
This function loads a new image into a UIImageView:
private func loadImage(targetView: UIImageView, imageURL: String) {
// Get file path to <imageURL>
let imageURL = getFilePath(imageURL)
// Create image & show in <targetView>
if let image = UIImage(named: imageURL) {
targetView.contentMode = .ScaleAspectFill
targetView.image = image
targetView.clipsToBounds = true
targetView.hidden = false
} else {
print("Error rendering image for \(imageURL)")
}
}
推荐答案
解决方案是在关闭时重置 UIImageView,因此第二个图像基本上重新加载到新的 UIImageView 中.
The solution was to reset the UIImageView upon closing so the second image is essentially reloaded into a fresh UIImageView.
这篇关于在显示新图像之前,在同一个 UIImageView 中加载多个图像会闪烁最后一个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!