[iPhone] Drawing images in drawRect

Discussion in 'iOS Programming' started by rand0m3r, Jul 3, 2008.

  1. macrumors regular

    Joined:
    Jun 30, 2006
    #1
    hi. i've got the following code in my drawRect but nothing is coming out on the screen:

    // Drawing the current person's display picture
    CGImageRef image = [[appDelegate.people objectAtIndex:appDelegate.currentPersonNum] image2];
    CGRect imageRect;
    imageRect.origin = CGPointMake(8.0, 8.0);
    imageRect.size = CGSizeMake(64.0, 64.0);
    CGContextDrawImage(context, imageRect, image);

    has anyone had any success with drawing images in your subclassed UIView? cheers.
     
  2. macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #2
    My guess is that the problem is in your image2 method. If you are able to post the source code for that, that is the first thing I would try to rule out.
     
  3. thread starter macrumors regular

    Joined:
    Jun 30, 2006
  4. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #4
    Have you checked to see if your image variable was nil or not?
     
  5. thread starter macrumors regular

    Joined:
    Jun 30, 2006
    #5
    u're right, it was null. i've got it work now, but what the hell it's upside down?!
     
  6. macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #6
    Page 38 of the Quartz 2D Programming Guide has one alternative. I'm not sure if isFlipped works when you're working outside the Cocoa drawing methods - you may need to transform the context as mentioned in the guide. If you are new to drawing with Quartz, I would recommend reading the guide before anything else.
     
  7. thread starter macrumors regular

    Joined:
    Jun 30, 2006
    #7
    transforming the context is a bad idea becoz everything else will become upside down and only my image will be the right way around.
     
  8. macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #8
    Only if you don't save the graphics state before the transform and restore it afterward.

    I haven't done a ton of work with Core Graphics functions, but I have always transformed the matrix before drawing any images. That said, the other drawing has also been done with the transformed matrix. Someone else may chime in with another alternative - I would love to know if there is a better way.
     
  9. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #9
    You could pre-flip your png's offline if you don't want to use affine transforms and save/restore CG state. You'd have to measure the performance hit of the latter.
     

Share This Page