Transparent background on iOS

Original
Transparent

procedure

1. Make a mask image.

Acquire mask images using segmentation models such as the DeepLab v3 model distributed by Apple.
Please refer to this article.

2. Make a transparent image.

Create a transparent image with the same size as the original image.
Since the “non-transparent area” is specified in the argument of settingAlphaOne, specify zero to acquire a fully transparent image.

let transparentImage = originalCIImage.settingAlphaOne(in: CGRect.zero)

3. Synthesize.

Foreground = original image, background = transparent image, mask image = mask image of the object you want to keep, and combine with CIFilter.

let compositeImage = CIFilter(name: "CIBlendWithMask", parameters: [
kCIInputImageKey: originalCIImage,
kCIInputBackgroundImageKey:transparentImage,
kCIInputMaskImageKey:mask]).outputImage
Composited

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store