This is the background image
This is the logo
How can we make a image like this in Swift?
Update
Now I manage to use the logo as a mask and get something like this,
Is there any way to reverse the mask?
Here is my code
let logo = UIImage(named: "logo")!
let mask = CALayer()
mask.contents = logo.CGImage
mask.frame = mImageView.layer.bounds
mImageView.layer.mask = mask
You can do it programmatically using UIBezierPath:
// lets create a view and an image for testing
let picture = UIImage(data: try! Data(contentsOf: URL(string: "http://i.stack.imgur.com/Xs4RX.jpg")!))!
// lets create a view and an image for testing
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: picture.size.width, height: picture.size.height))
imageView.image = picture
// now a layer for the mask
let maskLayer = CAShapeLayer()
// a path for the logo
let maskPath = CGMutablePath()
// create your logo path (I've added this circle to represent your logo path)
maskPath.addEllipse(in: CGRect(x: imageView.frame.midX - 150, y: imageView.frame.midY - 150, width: 300, height: 300))
// you will need a rectangle that covers the whole image area to intersect with your logo path
maskPath.addRect(CGRect(x: 0, y: 0, width: picture.size.width, height: picture.size.height))
// add the mask to your maskLayer
maskLayer.path = maskPath
// choose the fill rule EvenOdd
maskLayer.fillRule = kCAFillRuleEvenOdd
// add your masklayer to your view
imageView.layer.mask = maskLayer
imageView
If you need to use an image and invert the alpha of you logo programmatically you can do as follow using kCGBlendModeDestinationOut:
import UIKit
extension UIImage {
func masked(with image: UIImage, position: CGPoint? = nil, inverted: Bool = false) -> UIImage? {
let position = position ??
CGPoint(x: size.width.half - image.size.width.half,
y: size.height.half - image.size.height.half)
defer { UIGraphicsEndImageContext() }
UIGraphicsBeginImageContextWithOptions(size, false, scale)
draw(at: .zero)
image.draw(at: position, blendMode: inverted ? .destinationOut : .destinationIn, alpha: 1)
return UIGraphicsGetImageFromCurrentImageContext()
}
}
let picture = UIImage(data: try! Data(contentsOf: URL(string: "http://i.stack.imgur.com/Xs4RX.jpg")!))!
let logo = UIImage(data: try! Data(contentsOf: URL(string: "https://www.dropbox.com/s/k7vk3xvcvcly1ik/chat_bubble.png?dl=1")!))!
let view = UIView(frame: UIScreen.main.bounds)
view.backgroundColor = .blue
let iv = UIImageView(frame: UIScreen.main.bounds)
iv.contentMode = .scaleAspectFill
iv.image = picture.masked(with: logo, inverted: true)
view.addSubview(iv)
let bg = UIImage(named: "bg")!
let logo = UIImage(named: "logo")!
let size = imageView.frame.size
let rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContext(size)
bg?.drawInRect(rect)
logo.drawInRect(rect, blendMode: kCGBlendModeDestinationOut, alpha: 1.0)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
imageView.image = newImage
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With