2015-09-24 165 views
1

我正在使用帶有cameraSource類型的UIImagePickerController。但我注意到,有方栽跟頭不是與「移動和縮放」用UIImagePickerController iOS的圓形裁剪相機?

在iOS原生應用接觸有一個與「移動和縮放」稱號圓形栽跟頭圓形栽跟頭。但隨着我使用UIImagePickerController與相機源類型,我得到了一個沒有標題的方形裁剪器。

請推薦一些庫或代碼

這是iOS原生應用程序的屏幕截圖當用戶選擇拍照與攝像頭。我想同樣的

感謝

+0

你試過這個:http://stackoverflow.com/questions/9362712/uiimagepickercontroller-crops-picture-to-square-in-portrait? –

+0

@ Mr.T謝謝你的回覆。讓我檢查這個.. – user100

+0

@ Mr.T這個庫不適用於相機。只是顯示相機和默認cropper.Please建議我如何顯示圓形cropper後捕獲像iPhone本地聯繫人應用程序的照片。謝謝 – user100

回答

3

我會告訴我如何做的。

我創建了一個視圖控制器(PhotoPicker)用相機拍照並用他的圓形圖層裁剪它們。它包含一個工具欄,底部帶有重拍和完成按鈕,以及滾動視圖中的圖像視圖(請參閱我的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中調用此函數來創建裁剪。

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) 
    } 

然後我打電話ImagePickerviewDidAppear

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() 
    } 

正如你可以看到我打電話OverlayVC當我告訴了ImagePicker。這是我創建的另一個視圖,請參閱此處的截圖:http://imgur.com/6nr4PNW。但其網點也連接到PhotoPicker。不要創建其他課程,只需在故事板中查看並連接到PhotoPicker課程的按鈕&。

+0

感謝您的回覆。但我不明白。我在我的代碼中使用了objectivec。請編寫更多文檔。 – user100

+0

您需要在故事板(PhotoPicker)和另一個視圖中的xib文件(疊加層)中創建視圖控制器。兩者都連接到一個班級(PhotoPicker.swift),你有我給你的代碼,允許你拍攝和裁剪照片。但是我很抱歉,我不能在Obj-C中給你代碼。 –

+0

謝謝。當我在相機捕捉屏幕上它沒有顯示像iOS本機的全屏圖像捕捉聯繫人應用程序顯示請看看這個http://stackoverflow.com/questions/32822246/uiimagepickercontroller-camera-full-screen-capture-photo/ 32822476#32822476 – user100