Image repetition while drawing on UIImageView.

Discussion in 'iOS Programming' started by Aany, Jun 24, 2011.

  1. Aany, Jun 24, 2011
    Last edited by a moderator: Jun 24, 2011

    Aany macrumors newbie

    Joined:
    Jun 24, 2011
    Location:
    France
    #1
    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
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    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.
     
  3. Aany thread starter macrumors newbie

    Joined:
    Jun 24, 2011
    Location:
    France
    #3
    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?
     
  4. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #4
    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.
     
  5. Aany thread starter macrumors newbie

    Joined:
    Jun 24, 2011
    Location:
    France
    #5
    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.
     
  6. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #6
    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:.
     

Share This Page