1

I am trying to change the square crop tool to circular one for the images selected from Photo Library in Swift 4. I tried many old codes available in here with no luck. Can somebody please help me.

There are mainly 2 issues in this code.

  1. It doesn't hide the already existing square cropping area.
  2. It shows the choose and cancel buttons under the old overlay, so cant tap on it.

Any help would be appreciated.

My code:

private func hideDefaultEditOverlay(view: UIView)
{
    for subview in view.subviews
    {
        if let cropOverlay = NSClassFromString("PLCropOverlayCropView")
        {
            if subview.isKind(of: cropOverlay) {
                subview.isHidden = true
                break
            }
            else {
                hideDefaultEditOverlay(view: subview)
            }
        }
    }
}

private func addCircleOverlayToImageViewer(viewController: UIViewController) {
    let circleColor = UIColor.clear
    let maskColor = UIColor.black.withAlphaComponent(0.8)
    let screenHeight = UIScreen.main.bounds.size.height
    let screenWidth = UIScreen.main.bounds.size.width

    hideDefaultEditOverlay(view: viewController.view)

    let circleLayer = CAShapeLayer()
    let circlePath = UIBezierPath(ovalIn: CGRect(x: 0, y: screenHeight - screenWidth, width: screenWidth, height: screenWidth))
    circlePath.usesEvenOddFillRule = true
    circleLayer.path = circlePath.cgPath
    circleLayer.fillColor = circleColor.cgColor

    let maskPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight), cornerRadius: 0)
    maskPath.append(circlePath)
    maskPath.usesEvenOddFillRule = true

    let maskLayer = CAShapeLayer()
    maskLayer.path = maskPath.cgPath
    maskLayer.fillRule = kCAFillRuleEvenOdd
    maskLayer.fillColor = maskColor.cgColor
    viewController.view.layer.addSublayer(maskLayer)

    // Move and Scale label
    let label = UILabel(frame: CGRect(x: 0, y: 20, width: view.frame.width, height: 50))
    label.text = "Move and Scale"
    label.textAlignment = .center
    label.textColor = UIColor.white
    viewController.view.addSubview(label)
}
defiant
  • 3,161
  • 11
  • 41
  • 65

0 Answers0