Skip to content

Instantly share code, notes, and snippets.

@yuvalt
Last active January 30, 2016 23:06
Show Gist options
  • Save yuvalt/4113e19c89f5262f37ed to your computer and use it in GitHub Desktop.
Save yuvalt/4113e19c89f5262f37ed to your computer and use it in GitHub Desktop.
Hack to add circle to UIImagePickerController Credit: https://gist.github.com/andreacipriani/74ea67db8f17673f1b8b
/* Usage:
// This is in your viewcontroller, which implements UINavigationControllerDelegate
func navigationController(navigationController: UINavigationController, willShowViewController: UIViewController, animated: Bool) {
if isImageViewer(navigationController) {
addCircleOverlayToImageViewer(willShowViewController)
}
}
*/
func isImageViewer(navigationController: UINavigationController) -> Bool {
if (navigationController.viewControllers.count == 3 &&
(navigationController.viewControllers[2].dynamicType.description() == "PUUIImageViewController" ||
navigationController.viewControllers[2].dynamicType.description() == "PLUIImageViewController")) {
return true
}
return false
}
func addCircleOverlayToImageViewer(viewController: UIViewController) {
let circleColor = UIColor.clearColor()
let maskColor = UIColor.blackColor().colorWithAlphaComponent(0.8)
let screenHeight = UIScreen.mainScreen().bounds.size.height
let screenWidth = UIScreen.mainScreen().bounds.size.width
var plCropOverlayCropView: UIView? //The default crop view, we wan't to hide it and show our circular one
var plCropOverlayBottomBar: UIView? //On iPhone is the bar with "cancel" and "choose" button, on Ipad is an Image View with a label saying "Scale and move"
if UI_USER_INTERFACE_IDIOM() == .Pad {
plCropOverlayCropView = viewController.view.subviews[1]
plCropOverlayBottomBar = viewController.view.subviews[1].subviews[1]
// Protect against iOS changes...
if plCropOverlayCropView != nil && plCropOverlayCropView!.dynamicType.description() != "PLCropOverlay" {
print("Image Picker with circle overlay: PLCropOverlay not found")
return;
}
if plCropOverlayBottomBar != nil && plCropOverlayBottomBar!.dynamicType.description() != "UIImageView" {
print("Image Picker with circle overlay: PLCropOverlayBottomBar not found")
return;
}
}
else {
plCropOverlayCropView = viewController.view.subviews[1].subviews.first
plCropOverlayBottomBar = viewController.view.subviews[1].subviews[1]
// Protect against iOS changes...
if plCropOverlayCropView != nil && plCropOverlayCropView!.dynamicType.description() != "PLCropOverlayCropView" {
print("Image Picker with circle overlay: PLCropOverlay not found.")
return;
}
if plCropOverlayBottomBar != nil && plCropOverlayBottomBar!.dynamicType.description() != "PLCropOverlayBottomBar" {
print("Image Picker with circle overlay: PLCropOverlayBottomBar not found.")
return;
}
}
// It seems that everything is ok, we found the CropOverlayCropView and the CropOverlayBottomBar
plCropOverlayCropView!.hidden = true //Hide default CropView
let circleLayer = CAShapeLayer()
let circlePath = UIBezierPath(ovalInRect: CGRectMake(0, screenHeight/2 - screenWidth/2, screenWidth, screenWidth))
circlePath.usesEvenOddFillRule = true
circleLayer.path = circlePath.CGPath
circleLayer.fillColor = circleColor.CGColor
// Mask layer frame: it begins on y=0 and ends on y = plCropOverlayBottomBar.origin.y
let maskPath = UIBezierPath(roundedRect: CGRectMake(0, 0, screenWidth, screenHeight), cornerRadius: 0)
maskPath.appendPath(circlePath)
maskPath.usesEvenOddFillRule = true
let maskLayer = CAShapeLayer()
maskLayer.path = maskPath.CGPath
maskLayer.fillRule = kCAFillRuleEvenOdd
maskLayer.fillColor = maskColor.CGColor
viewController.view.layer.addSublayer(maskLayer)
// Re-add the overlayBottomBar with the label "scale and move" because we set its parent to hidden (it's a subview of PLCropOverlay)
viewController.view.addSubview(plCropOverlayBottomBar!)
}
@yuvalt
Copy link
Author

yuvalt commented Jan 30, 2016

I diverged a bit from the original. You're welcome to look at the history of the gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment