用UIImagePickerController iOS圆形裁剪相机?

我正在使用cameraSourcetypes的UIImagePickerController。 但我注意到有一个圆形的收割机,而有一个圆形收割机,带有“移动和缩放”

在iOS本地联系人应用程序中,有一个带有“移动和缩放”标题的圆形裁剪器。 但与我使用UIImagePickerController相机源types,我有一个没有标题的方形裁剪。

请build议一些图书馆或代码

这是用户select用相机拍照时的iOS本地应用程序屏幕截图。 我想要

谢谢

我会展示我是如何做到的。

我创build了一个视图控制器( PhotoPicker ),用相机拍摄照片并用他的圆形图层裁剪它们。 它包含一个工具栏在底部与重拍和完成button和滚动视图中的图像视图(请参阅我的PhotoPicker的屏幕截图: http ://imgur.com/MleuKmh)。

当我想拍摄一张照片时,我用下面的代码打开PhotoPicker

 let photoPickerVC=self.storyboard?.instantiateViewControllerWithIdentifier("photoPickerVC") as! PhotoPicker photoPickerVC.modalPresentationStyle=UIModalPresentationStyle.OverFullScreen self.presentViewController(photoPickerVC, animated: true, completion: nil) 

我的PhotoPicker类将需要UIImagePickerControllerDelegate拍照。

我在viewDidLoad调用这个函数来创build裁剪。

 func addCropLayer(){ //layer circle let circleLayer=CAShapeLayer() let squareLength=self.view.frame.width - 40 innerrect=CGRectMake(20, self.view.center.y - (squareLength/2) - self.toolBar.frame.height, squareLength, squareLength) let path2=UIBezierPath(ovalInRect: innerrect) path2.usesEvenOddFillRule=true circleLayer.path=path2.CGPath circleLayer.fillColor=UIColor.clearColor().CGColor let path=UIBezierPath(roundedRect: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height - toolBar.frame.height), cornerRadius: 0) path.appendPath(path2) path.usesEvenOddFillRule=true //black layer that contains the transparent circle //var fillLayer=CAShapeLayer() fillLayer.path=path.CGPath fillLayer.fillRule=kCAFillRuleEvenOdd fillLayer.fillColor=UIColor.blackColor().CGColor fillLayer.opacity=0.8 self.view.layer.addSublayer(fillLayer) self.view.addSubview(toolBar) } 

然后我调用viewDidAppearImagePicker

 override func viewDidAppear(animated: Bool) { self.showImagePickerForSourceType(UIImagePickerControllerSourceType.Camera) } func showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType){ print("showImagePickerForSourceType") if self.imageView.isAnimating(){ self.imageView.stopAnimating() } //var capturedImages=NSMutableArray() if self.capturedImages.count>0 { self.capturedImages.removeAllObjects() } let imagePickerController=UIImagePickerController() imagePickerController.modalPresentationStyle=UIModalPresentationStyle.CurrentContext imagePickerController.sourceType=sourceType imagePickerController.delegate=self imagePickerController.cameraDevice=UIImagePickerControllerCameraDevice.Front if sourceType == UIImagePickerControllerSourceType.Camera { imagePickerController.showsCameraControls=false NSBundle.mainBundle().loadNibNamed("OverlayVC", owner: self, options: nil) self.overlayView.frame=UIScreen.mainScreen().bounds imagePickerController.cameraOverlayView=self.overlayView self.overlayView=nil } print("presentviewcontroller") self.imagePickerController=imagePickerController let screenSize:CGSize=UIScreen.mainScreen().bounds.size let cameraAspectRatio:CGFloat=3.0/4.0 print("camera aspect ratio: \(cameraAspectRatio)") let scale=(screenSize.height / screenSize.width) * cameraAspectRatio print("scale: \(scale)") let yOffset=(screenSize.height - screenSize.width / cameraAspectRatio)/2 print("y offset: \(yOffset)") let translate:CGAffineTransform=CGAffineTransformMakeTranslation(0, yOffset) let fullScreen:CGAffineTransform=CGAffineTransformMakeScale(scale, scale) let fullTransform:CGAffineTransform=CGAffineTransformConcat(fullScreen, translate) self.imagePickerController.cameraViewTransform=fullTransform let iOS:NSString=UIDevice.currentDevice().systemVersion let iOSint:Int=iOS.integerValue print("iOS int value: \(iOSint)") if iOSint < 8 { print("ios < 8") var rect:CGRect=self.imagePickerController.view.frame rect.size.height = screenSize.width / cameraAspectRatio rect.size.width = screenSize.width print("rect: \(rect)") self.imagePickerController.view.frame=rect self.imagePickerController.view.transform=fullTransform }else{ self.imagePickerController.view.frame=UIScreen.mainScreen().bounds } self.presentViewController(self.imagePickerController, animated: true, completion: nil) } @IBAction func takePhoto(sender: AnyObject) { print("takePhoto") self.imagePickerController.takePicture() } 

正如你所看到的,当我显示ImagePicker时,我调用了OverlayVC 。 这是我创build的另一种观点,请看这里的截图: http : //imgur.com/6nr4PNW 。 但它的sockets也连接到PhotoPicker 。 不要创build另一个类,只是故事板中的视图,并将button连接到PhotoPicker类。