UIImage fill transparent part with another image

后端 未结 1 1228
梦毁少年i
梦毁少年i 2021-01-25 13:27

I would like to fill the transparent part of an image with another image in iOS.

I have tried some stuff with UIGraphicsContext, but I can\'t get it working

1条回答
  •  无人及你
    2021-01-25 13:30

    Here's how I would do it:

    You can make a clipping path in the shape of the circle by doing something like this:

    CGContextSaveGState(context);
    CGRect circleRect = CGRectMake(circleCenter.x - radius, circleCenter.y - radius, radius * 2.0, radius * 2.0);
    CGContextAddEllipseInRect (context, circleRect);
    CGContextClip(context);
    
    // ... do whatever you need to in order to draw the inner image ...
    
    CGContextRestoreGState(context);
    
    // ... draw the transparent png ...
    

    Setting a clipping path will cause drawing to only happen within the path. When you restore the state, it removes the clipping path (or rather sets it back to its previous value which is usually allowing it to draw to the entire canvas).

    0 讨论(0)
提交回复
热议问题