Drawing a transparent circle on top of a UIImage - iPhone SDK

前端 未结 4 535
轮回少年
轮回少年 2021-02-11 07:19

I am having a lot of trouble trying to find out how to draw a transparent circle on top of a UIImage within my UIImageView. Google-ing gives me clues, but I still can\'t find a

4条回答
  •  孤街浪徒
    2021-02-11 08:10

    You can implement a custom sub-class of UIView that draws your image and then the circle in the drawRect method:

    @interface CircleImageView : UIView {
        UIImage * m_image;
        CGRect m_viewRect;
    
        // anything else you need in this view?
    } 
    

    Implementation of drawRect:

    - (void)drawRect:(CGRect)rect {
    
        // first draw the image
        [m_image drawInRect:m_viewRect blendMode:kCGBlendModeNormal alpha:1.0];
    
        // then use quartz to draw the circle
        CGContextRef context = UIGraphicsGetCurrentContext ()
    
        // stroke and fill black with a 0.5 alpha
        CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 0.5);
        CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.5);
    
        // now draw the circle
        CGContextFillEllipseInRect (context, m_viewRect);
    }
    

    You will need to set up the m_viewRect and m_image member functions on init.

提交回复
热议问题