33543

Loading multiple images in same UIImageView flashes last image before showing new image

Question:

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.

Example user flow:

<ul><li>User taps button for image 1. UIImageView appears loaded with image 1.</li> <li>User taps different button to close UIImageView. UIImageView gets hidden.</li> <li>User taps different button for image 2. UIImageView is loaded with image 2 then unhidden. Image 1 appears for a moment before image 2 appears.</li> </ul>

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?

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)") } }

Answer1:

The solution was to reset the UIImageView upon closing so the second image is essentially reloaded into a fresh UIImageView.

Answer2:

I am not exactly sure what you are trying to accomplish, but this certainly should get you on your way to a solution. I tried to make it as deatailed as possible, but I can answer any questions you have.

class RandomImage: UITableViewCell { @IBOutlet weak var ImageView: UIImageView! @IBOutlet weak var ChangingButton: UIButton! override func awakeFromNib() { super.awakeFromNib() // Initialization code self.ChangingButton.setTitle("Change", forState: UIControlState.Normal) } @IBAction func changeImageTapped(sender: UIButton){ var firstRandomNumber = arc4random_uniform(NUMBER) + 1 var firstImageString:String = String(format: "image%i", firstRandomNumber) self.ImageView.image = UIIMAge(named: firstImageString) }

}

After connecting your outlets and adjusting the needed information in the code you should get a button that when clicked would return a random image after every click. I hope this can help you out.

Cheers.

Recommend

  • Reading cookies from server from another domain
  • How to disable a textbox if a checkbox is checked using jQuery?
  • What is a better way to implement a system wide side panel (similar to navigation drawer) in Android
  • Play music in background
  • Reverse animation on a second click
  • converting png to c header for sdl
  • Cancel a long task that's managed by a web service
  • Create ListView ScrollBar Appeared Event
  • For loop and gsub R
  • GraphicsMagick for node not masking
  • openOptionsMenu() across android versions
  • WooCommerce Free Shipping - Remove raw or change the text name on checkout and email
  • How would I make a polygon based on the relative positions of markers in google maps?
  • Split an Array into 3 arrays [duplicate]
  • When exactly does Mongoid actually make a query to MongoDb
  • how to change button text after succes in ajax
  • Getting CKEditor to work with Flask Admin
  • UIImage to UIColor array of pixel colors
  • Rust lifetime error
  • Refresh JSF component after custom javascript Ajax call
  • Tkinter tkMessageBox disables Tkinter key bindings
  • How to control xtics in gnuplot
  • 2d barcode reader Java ME sdk
  • Run a form (insert/update/delete) from within a div using jquery
  • How to make Rss News Reader application in android …? [closed]
  • How to resolve this in PHPUnit where it is asking me to set KERNEL_DIR in my phpunit.xml?
  • read part of h5 dataset python
  • How to call different template for different category archive page in woocommerce
  • WPF custom control and direct content support
  • Write to .csv file with PHP (Commas in Data Error)
  • Create/delete users from text file using Bash script
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}
  • Time Complexity of Fibonacci Algorithm [duplicate]
  • How to check if object is null in Java?