Forcing a UIImagePickerController to open a square camera with Swift

I struggled to find a way to launch the UIImagePickerController with a square live view and get it to make a square picture. The code that I have now to start the camera is below

func takePicture() { if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){ println("Button capture") var imag = UIImagePickerController() imag.delegate = self imag.sourceType = UIImagePickerControllerSourceType.Camera //imag.mediaTypes = [kUTTypeImage]; imag.allowsEditing = false self.presentViewController(imag, animated: true, completion: nil) } } func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) { let selectedImage : UIImage = image //var tempImage:UIImage = editingInfo[UIImagePickerControllerOriginalImage] as UIImage var newImage: UIImage = resizeImageWithImage(selectedImage, size: CGSize(width: 320, height: 568)) datImage.image=newImage let paths: NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) let documentsDir: NSString = paths.objectAtIndex(0) as NSString var dateFormat = NSDateFormatter() dateFormat.dateFormat = "yyyy-MM-dd-HH-mm-ss" let now:NSDate = NSDate(timeIntervalSinceNow: 0) let theDate = dateFormat.stringFromDate(now) //Make Photo URL self.photoURL = NSString(format: "/%@.png", theDate) //save resized image let pathToFullPhoto: NSString = documentsDir.stringByAppendingString(self.photoURL) let pngFull: NSData = UIImagePNGRepresentation(newImage) pngFull.writeToFile(pathToFullPhoto, atomically: true) self.dismissViewControllerAnimated(true, completion: nil) } 

starts with: self.takePicture()

Any help would be appreciated.

+6
source share
1 answer

Have you looked at the cameraOverlayView property? This seems to be what you are looking for.

Link to documents

Then you need to crop the image after receiving it, since overlay overlay will not crop. To crop, just get the image and draw it in a rectangle that is smaller, depending on the size of the โ€œopeningโ€ in your cameraOverlayView .

0
source

Source: https://habr.com/ru/post/980238/


All Articles