1

I am trying to draw a line using multiple UIImageView of the same circle image.

I have tried the following:

let imageName = "circle.jpg" let image = UIImage(named: imageName) for var i in 0..<100 { i += 1 let imageView = UIImageView(image: image!) imageView.frame = CGRect(x: i, y: 200, width: 25, height: 25) view.addSubview(imageView) } 

This produces one circle at the position (100, 200). This happens because the same UIImageView is being added to the subview so it is only updating the position rather than adding a new UIImageView. If I create a new UIImageView named "imageView1" and add it to the subview, it will create a new circle.

For the purpose of forming a line by overlapping the UIImageViews of these circles, manually creating a UIImageView for each circle is obviously inefficient.

So, how can I use the same UIImage to draw multiple UIImageViews? Any other suggestions on how I can get around accomplishing this?

2
  • try by adding code of for loop in autoreleasepool.(make sure that-only inner code will be inside the pool, but for loop is outside the pool) Commented Oct 25, 2016 at 7:40
  • And what's the point of autorelease pool here? It will do literally nothing Commented Oct 25, 2016 at 9:01

3 Answers 3

1

You need a circle to form a line like this

enter image description here

Well here the code I used for this using While loop

let imageName = "synergy-many-people-turning-in-gears_gg55375354.jpg" let image = UIImage(named: imageName) var i = 0 while(i<100) { let imageView = UIImageView(image: image!) imageView.frame = CGRect(x: i, y: 200, width: 25, height: 25) view.addSubview(imageView) i = i + 25//Here I set it to the width so that the images don't overlap //you can use any value for the desired effect it just sets the location //of X-Co-ordinate where the next image would be added } 
Sign up to request clarification or add additional context in comments.

Comments

0

I would quickly try out something like this (didn't test it though):

var imageView = UIImageView(image: image!)

Comments

0

Creating multiple UIImageViews is the thing you need to do. If this will be to slow (btw you should place your image inside assets catalogue to allow caching and improve performance), you should switch to lower level api. Check iOS - An easy way to draw a circle using CAShapeLayer And, btw, why do you change i variable inside loop, this is very bad practice

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.