I have created below masked image and I have masked image like this
Here is my code
//
// Collage_Layout_1.swift
// Snapcial
//
// Created by Jecky Modi on 15/02/25.
//
class Collage_Layout_1: CollageLayoutBaseView {
var maskedImage: String = ""
var originalImage = UIImage()
private let maskImageView = UIImageView()
var viewFrame : CGRect = .zero
// private var originalImageCenter: CGPoint?
init(frame: CGRect, maskedImage: String, originalImage: UIImage) {
super.init(frame: frame)
self.maskedImage = maskedImage
self.originalImage = originalImage
self.viewFrame = frame
}
override func setUpCollage() {
let maskedContainer = self.arrViews[0].containerView
maskedContainer.tag = 100
let maskImage = UIImage(named: maskedImage)?.withRenderingMode(.alwaysTemplate)
maskImageView.translatesAutoresizingMaskIntoConstraints = false
maskImageView.image = maskImage
maskImageView.contentMode = .scaleAspectFit
maskImageView.tintColor = .clear
addSubview(maskImageView) // Keep the mask image visible
// Create a mask layer from the black shape
let maskLayer = CALayer()
maskLayer.contents = maskImage?.cgImage
maskLayer.frame = self.viewFrame
maskLayer.contentsGravity = .resizeAspect
maskedContainer.layer.mask = maskLayer
maskedContainer.clipsToBounds = true
maskedContainer.backgroundColor = .clear
self.backgroundColor = .clear
addSubview(maskedContainer) // Add container with mask
NSLayoutConstraint.activate([
maskImageView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
maskImageView.topAnchor.constraint(equalTo: self.topAnchor),
maskImageView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
maskImageView.bottomAnchor.constraint(equalTo: self.bottomAnchor),
maskedContainer.leadingAnchor.constraint(equalTo: self.leadingAnchor),
maskedContainer.topAnchor.constraint(equalTo: self.topAnchor),
maskedContainer.trailingAnchor.constraint(equalTo: self.trailingAnchor),
maskedContainer.bottomAnchor.constraint(equalTo: self.bottomAnchor),
])
self.layoutIfNeeded()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
I want to add borders to my masked image that border should take shape of mask.
Any help would be appreciated.





CGPathorUIBezierPath). So, you could (a) create a B&W rendition of the mask image; (b) perform “edge detection” (stackoverflow.com/questions/78359266/78360887#78360887) to get a path from this B&W rendition of the mask image; (c) create a view with aCAShapeLayerto render this path; and (d) place this view above the image view. It’s a lot of work (both programmatically & computationally), so ask whether you really want to do that, or whether you can just start with a path, and eliminate this “get path from image alpha” overhead.