Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Aany

macrumors newbie
Original poster
Jun 24, 2011
7
0
France
Hi,
I am developping an app which calculates camera lens parameters. It has an area which shows a camera view where user can drag and set camera distance etc. I want to draw a triangle between a fixed point and two others which move when user touches and drags them. I managed to draw this triangle but the problem is that every time user touches or moves the dot a new triangle appears while i only want one. I used the following code:
in touchesMoved:

Code:
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0,0,0,1);
CGPoints[CGPointMake(0,100), CGPointMake(location.x,location.y), CGPointMake(location.x,location.y), CGPointMake(newLoc.x,newLoc.y), CGPointMake(newLoc.x,newLoc.y), CGPointMake(0,100)]
//draw is my imageView
draw.image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext()

Thanks in advance
 
Last edited by a moderator:
Your code doesn't tell us much. It suggests you are drawing the triangle right into the image you want to overlay it on. Also, I highly doubt you actually copied and pasted real code into your post. The give away is the 'CGPoints' line.

What is drawImage.image and draw.image? Where is draw.image being used? What is the context?

I'm thinking you want to draw a triangle that moves via a touch & drag. Given that, I think what you want is for the triangle to be a subview. As it receives a touch, you alter the center point property causing it to move.
 
Well i did not copy any code and that line on CGPoints was a mistake. And yes you are right that's exactly what I want to do. Actually I first tried to draw a triangle using makeRect function but the problem is that it draws the triangle once but doesn't redraw everytime the dot is dragged. What I did was tell the function to draw a triangle using the same code
Code:
CGPoint points[]=CGPoint[CGPointMake(0,100), CGPointMake(location.x,location.y), CGPointMake(location.x,location.y), CGPointMake(newLoc.x,newLoc.y), CGPointMake(newLoc.x,newLoc.y), CGPointMake(0,100)];
CGContextStrokeLineSegments(UIGRaphicsGetCurrentContext, points, 6);
Where location and newLoc are points that should be upgraded as the dot is dragged. I calculate and upgrade them in touchesMoved function. I am sure tehre is something I am missing but what?
 
Well i did not copy any code and that line on CGPoints was a mistake.

And it is still a mistake in that it does not compile. See the red hi-lites.
Code:
CGPoint points[] = [COLOR="red"][B]{[/B][/COLOR]CGPointMake(0,100), CGPointMake(location.x,location.y), CGPointMake(location.x,location.y), CGPointMake(newLoc.x,newLoc.y), CGPointMake(newLoc.x,newLoc.y), CGPointMake(0,100)[COLOR="Red"][B]}[/B][/COLOR];

Always cut and paste from the code you are having issues with.


You did not answer my questions regarding drawImage.image and draw.image.


I re-read your posts and after looking at your points line I think I was wrong about what you want. It looks like perhaps you want to grab one of the points of the triangle and move just that point and redraw to it. But you mentioned a fixed point, so I'm confused.

Perhaps a trick to zero in on the issue would be to alter the color of the triangle line each time through that code. My suspicion is that you are compositing the triangle onto another image but have not shown us that code. Or perhaps you are adding each triangle onto a superview but not removing the previous triangle subview. You have not shared enough information for a proper response.
 
drawImage and draw are UIImageViews.
When I compile I don't get any error for points :confused:.

When I talked about a fixed point I meant that I have one fixed point while other two can move. Actually I move one of the points and the other one's position is calculated according to the first one.
What I am trying to do is simply draw on a UIImageView (draw here) as I already explained that drawing directly with makeRect didn't work for me.
I am sorry for not being clear.
And thanks a lot for your help and suggestions.
 
I've made some suggestions and the obvious one is that you are repetitively doing an action without clearing a previous version of it. For instance, compositing draw.image onto drawImage.image.

I can't think of anything to add.

Since you refuse to supply plenty of your code, it isn't possible to be very helpful. You also didn't explain how drawImage and draw are related. You mentioned makeRect, but I suspect you mean drawRect:.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.