Skip to content

Instantly share code, notes, and snippets.

View jarakys's full-sized avatar

Kirill Chernov jarakys

View GitHub Profile
/// Remove background of input image based on an alpha mask.
///
/// - Parameters:
/// - image: Image to mask
/// - mask: Input mask. Reduces pixel opacity by mask alpha value. For instance
/// an alpha value of 255 will be completely opaque, 0 will be completely transparent
/// and a value of 125 will be partially transparent.
/// - Returns: Image mask with background removed.
func createMask(of image: UIImage, fromMask mask: UIImage, withBackground background: UIImage? = nil) -> UIImage? {
guard let imageCG = image.cgImage, let maskCG = mask.cgImage else { return nil }