Skip to content

Instantly share code, notes, and snippets.

@hamin
Created June 5, 2015 20:31
Show Gist options
  • Save hamin/e8c6dfe00d9c81375f3e to your computer and use it in GitHub Desktop.
Save hamin/e8c6dfe00d9c81375f3e to your computer and use it in GitHub Desktop.
// Discussion on how to do custom cropping overlay on UIImageViewPicker
// https://gist.github.com/Raiden996/74ea67db8f17673f1b8b
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
println("viewcontrollers count: \(navigationController.viewControllers.count)")
if navigationController.viewControllers.count == 3 {
println("viewcontrollers name: \(navigationController.viewControllers[2].classString())")
if (navigationController.viewControllers[2].classString() == "PUUIImageViewController") || (navigationController.viewControllers[2].classString() == "PLUIImageViewController") {
addOverlayToImagePicker(viewController)
}
}
}
func addOverlayToImagePicker(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 = UIView() //The default crop view, we wan't to hide it and show our circular one
var plCropOverlayBottomBar:UIView = 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() == UIUserInterfaceIdiom.Pad {
plCropOverlayCropView = viewController.view.subviews[1] as! UIView//[viewController.view.subviews objectAtIndex:1];
plCropOverlayBottomBar = viewController.view.subviews[1].subviews[1] as! UIView //[[[[viewController.view subviews] objectAtIndex:1] subviews] objectAtIndex:1];
//Protect against iOS changes...
if plCropOverlayCropView.classString() != "PLCropOverlay" {//(! [[[plCropOverlayCropView class] description] isEqualToString:@"PLCropOverlay"]){
println("Image Picker with circle overlay: PLCropOverlay not found")
return;
}
if plCropOverlayBottomBar.classString() != "UIImageView"{ //(! [[[plCropOverlayBottomBar class] description] isEqualToString:@"UIImageView"]){
println("Image Picker with circle overlay: PLCropOverlayBottomBar not found")
return;
}
} else {
plCropOverlayCropView = viewController.view.subviews[1].subviews.first as! UIView//[[[viewController.view.subviews objectAtIndex:1] subviews] firstObject];
plCropOverlayBottomBar = viewController.view.subviews[1].subviews[1] as! UIView//[[[[viewController.view subviews] objectAtIndex:1] subviews] objectAtIndex:1];
//Protect against iOS changes...
if plCropOverlayCropView.classString() != "PLCropOverlayCropView" {//(! [[[plCropOverlayCropView class] description] isEqualToString:@"PLCropOverlay"]){
println("Image Picker with circle overlay: PLCropOverlay not found")
return;
}
if plCropOverlayBottomBar.classString() != "PLCropOverlayBottomBar"{ //(! [[[plCropOverlayBottomBar class] description] isEqualToString:@"UIImageView"]){
println("Image Picker with circle overlay: PLCropOverlayBottomBar not found")
return;
}
}
plCropOverlayCropView.hidden = true //Hide default CropView
let circleLayer = CAShapeLayer()
//Center the circleLayer frame:
let circlePath = UIBezierPath(ovalInRect: CGRect(x: 0, y: screenHeight/2 - screenWidth/2, width: screenWidth, height: 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: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight - (plCropOverlayBottomBar.frame.size.height * 2)), 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)
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Phone {
//On iPhone add an hint label on top saying "scale and move" or whatever you want
let cropLabel = UILabel(frame: CGRectMake(0, 10, screenWidth, 50))
cropLabel.text = "FOO"//NSLocalizedString("IMAGE_PICKER_CROP_LABEL", caption: "")
cropLabel.textAlignment = NSTextAlignment.Center
cropLabel.textColor = UIColor.whiteColor()
viewController.view.addSubview(cropLabel)
} else {
//On iPad 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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment