ios – how to attract border to a masked picture utilizing swift?

0
2
ios – how to attract border to a masked picture utilizing swift?


I’ve created beneath masked picture

image

Right 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()
    non-public let maskImageView = UIImageView()
    var viewFrame : CGRect = .zero
    //    non-public var originalImageCenter: CGPoint?
    
    init(body: CGRect, maskedImage: String, originalImage: UIImage) {
        tremendous.init(body: body)
        self.maskedImage = maskedImage
        self.originalImage = originalImage
        self.viewFrame = body
    }
    
    override func setUpCollage() {
        let maskedContainer = self.arrViews[0].containerView
        maskedContainer.tag = 100
                
        let maskImage = UIImage(named: maskedImage)?.withRenderingMode(.alwaysTemplate)
        maskImageView.translatesAutoresizingMaskIntoConstraints = false
        maskImageView.picture = maskImage
        maskImageView.contentMode = .scaleAspectFit
        maskImageView.tintColor = .clear
        addSubview(maskImageView)       // Preserve the masks picture seen
        
        // Create a masks layer from the black form
        let maskLayer = CALayer()
        maskLayer.contents = maskImage?.cgImage
        maskLayer.body = self.viewFrame
        maskLayer.contentsGravity = .resizeAspect
        
        maskedContainer.layer.masks = maskLayer
        maskedContainer.clipsToBounds = true
        maskedContainer.backgroundColor = .clear
        self.backgroundColor = .clear
        
        addSubview(maskedContainer)     // Add container with masks
        
        
        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 carried out")
    }
}

I wish to add borders to my masked picture that border ought to take form of masks.

Any assist can be appreciated.

LEAVE A REPLY

Please enter your comment!
Please enter your name here