I am making a Swift 3 app that generates thousands of motionless UIImages to make a pattern. The pattern is created by iteration. The dots are 4x4 low-resolution images, located randomly. My app runs at one hundred dots per second using a clock until about 3000, where the lag becomes visible, and in next twenty thousand, it gets to forty-fifty seconds per thousand, versus ten. The gist of my generation is this:
@IBAction func AddPoint(_ sender: UIButton) {
let newdot = UIImageView(image: UIImage(named: "redDot"))
self.view.addSubview(newdot)
// lastx and lasty are edited here, before the dot is plotted.
newdot.frame = CGRect(x: lastx - newdotsize / 2.0, y: lasty - newdotsize / 2.0, width: newdotsize, height:newdotsize)
}
The code is fully functional, but the images are left on the view (as far as I know) untouchable. What can I do to speed it up? Should I merge them every thousand? If I am going to merge them, then I need flexible code (How to merge two UIImages? has only two images) and I need to be able to control them, which I don't know how to do.