1

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.

Scott V.
  • 11
  • 2

1 Answers1

0

Don't use UIImageViews to draw pixels. Use drawrect in UIView and core graphics

iOS draw filled Circles

Nick Wilkerson
  • 353
  • 2
  • 17
  • I will use the concepts from (https://stackoverflow.com/questions/17038017/ios-draw-filled-circles) and apply them to Swift. Thanks – Scott V. Jun 08 '17 at 03:46
  • I used the code from https://stackoverflow.com/questions/40555462/how-to-draw-a-circle-in-swift-3, but it still slows at about 3000. I think it is simply the amount of objects that kills it. When I first made it, my images were 4 (8x8 image) or 16 (16x16 image) times larger in area. I realized this could be slowing it down, as my dots were only four by four pixels on the screen. When I changed it, I saw no difference. Did I use the same "drawrect" code you suggested, or something else? – Scott V. Jun 08 '17 at 14:55