Drawing an image is completely out :(

Discussion in 'iOS Programming' started by ct2k7, Apr 7, 2010.

  1. ct2k7 macrumors 603

    ct2k7

    Joined:
    Aug 29, 2008
    Location:
    London or Florida
    #1
    Hi I'm using this code below to let a user "draw" their signature on a UIView component on my app:

    Code:
        UIGraphicsBeginImageContext(signature.frame.size);
        [drawImage.image drawInRect:CGRectMake(0, 0, signature.frame.size.width, signature.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext()
    The issue I am having is that the drawing is not only is the drawing not in line with the pointer when in use in the simulator, the image is going outside of the designated UIView compontent and is getting smaller/out of focus the more I draw, as you can see in this image:

    [​IMG]

    And after a few lines, showing the boundaries of the exact are of where I can draw:

    [​IMG]

    Any ideas on what's happening here?

    lastPoint.y is defined as: lastPoint.y -= 20;

    Any ideas on what on earth is happening here?
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    If you can post a simplified project that demos this, that'd be good. Right now it's hard to tell what's going on. I think the problem is not in the code you posted.

    Regarding your code, a few tips:
    - Don't keep calling UIGraphicsGetCurrentContext(). Call it once, store the result and use that instead.
    - You shouldn't need to use CGContextFlush(). Let the OS flush when it needs to, which it does for you automatically when your drawRect: method is done.
     
  3. ct2k7 thread starter macrumors 603

    ct2k7

    Joined:
    Aug 29, 2008
    Location:
    London or Florida
    #3
    Done and Done :)

    http://dl.dropbox.com/u/1545603/TEST_DRAW_APP.zip
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Code:
    drawImage.frame = signature.frame;
    That should be signature.bounds, not frame. If you're adding a view as a subview into another view, you need to set its frame in relation to its superview's bounds.

    Then remove these lines:

    Code:
    lastPoint.y -= 20;
    currentPoint.y -= 20;
    Finally, the view in your nib is somehow screwy. Its frame is decimal values. I had to resize/reposition it a bit to get it fixed. Not sure how it got like that. But fixing that prevents the images doing that weird fade.
     
  5. ct2k7 thread starter macrumors 603

    ct2k7

    Joined:
    Aug 29, 2008
    Location:
    London or Florida
    #5
    I've done as you've said, but now, I can't even draw on the signature UIView :(

    EDIT: I love you kainjow! It works!
     

Share This Page