Cropping a UIImage with a circle

Discussion in 'iOS Programming' started by drf1229, Jan 16, 2010.

  1. drf1229 macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #1
    I was curious if there is a way to crop a UIImage with a circle, so as to cut out only a person's head in a picture, etc. I've had success with CGImageCreateWithImageInRect() but I was curious if there was a way to do this with a circle. I think I need to create a mask but I'm not sure. Any advice is greatly appreciated!
     
  2. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #2
    If your original bitmap is 100 x 100, and the smallest rectangle that can enclose the circle is 50 x 50, then you can take that small bitmap and "cut the corners off" in a couple of different ways.

    The best way I can think of is to clip your graphics context. Basically, you have this small bitmap and you are going to draw that into a graphics context, but before you draw you tell the context where it "won't be drawing". These spots will show up as transparent (the rounded corners).

    You can clip your graphics context with a path (draw the circle with code), or with a bitmap mask. A mask would look like a black page with a white circle. The white part is drawn and the black part is not.

    Take a look at the Quartz 2D Programming Guide and there will be more about all of this.
     
  3. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #3
    Thanks for your reply! I'll look at the Quartz 2D guide
     

Share This Page