Drawing a UIImage as result of drawRect:

Discussion in 'iOS Programming' started by dantastic, Feb 14, 2014.

  1. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #1
    First of all, I think I might be going about this the wrong way so bare with me.

    I have a UIView where I draw a bunch of stuff using UIBezierPaths in the drawRect:. I need this drawing as a UIImage.

    First problem: Before the drawRect: has actually done its job I don't know the size required for the UIView. So I init the view with an arbitrary size with the intention to crop it after. At the end of the drawRect: I know the bounds of the drawing though.

    Second, this is what seems most horrible to me, I need to alloc&init my view, add it as a subview of a visible view. Exit the runloop and then I can take a screenshot by view.layer renderInContext:


    So I'm thinking the drawRect: of a UIView is not the right place for me to be drawing stuff if I want a UIImage after. But if it isn't, what is? What's the better approach here?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
  3. dantastic thread starter macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #3
    Cheers but that's pretty much the same method I'm doing already.

    I think what I will do is create a couple of UIView subclasses where I do less in each drawRect:
    Then I can create the views I need from the various bezier paths and just add them to (at this point a known size) a regular UIView and I can create an image out of that one.
     

Share This Page