swift 2 open camera in square mode only - ios

So far I have the camera working fine, it opens up in the regular format. But I want the only option to be square format.
Here is my code so far.
#IBAction func takePhoto(sender: AnyObject) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
presentViewController(picker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
dismissViewControllerAnimated(true, completion: nil)
}

I don't think you can open camera in square format using UIImagePickerController. What you can do is set UIImagePickerController allowEditing to YES and from the result dict use the key UIImagePickerControllerEditedImage then you will have the squared image so you code becomes .
#IBAction func takePhoto(sender: AnyObject) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
picker.allowsEditing = true
presentViewController(picker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
imageView.image = info[UIImagePickerControllerEditedImage] as? UIImage
dismissViewControllerAnimated(true, completion: nil)
}
Another way around is mentioned in this answer .
You can use the logic from answer to write the code in swift.
But if you want to look in details and try out doing some more have a look into
AVFoundation,
AVCaptureConnection,
AVCaptureDevice
AVCaptureDeviceInput
AVCaptureSession
AVCaptureStillImageOutput
AVCaptureVideoDataOutput
AVCaptureVideoPreviewLayer
CoreImage
Also look at apple sample.

Related

iOS, Swift, UIImagePicker allows editing crop tool removes transparency. Is there fix?

When I get PNG image with transparent background from UIImagePicker it inserts the image with the transparent background just fine as below:
when I set:
imagePicker.allowsEditing = true
it allows me to crop as expected in the view provided by UIImagePicker
but then when I press 'Choose' it returns the image with black instead of a transparent background as below:
The only change between the two results is setting
imagePicker.allowsEditing = true
and cropping the image in the app.
Is there a way to keep transparency or at least change the black to white when cropping?
I have searched the internet and no mention of this that I can see.
Code:
func showImagePicker() {
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
compImageView?.contentMode = .scaleAspectFit
compImageView?.backgroundColor = UIColor.clear
compImageView?.image = pickedImage
}
dismiss(animated: true, completion: nil)
}
By default UIImagePickerController removes the alpha channels in the edited image. To get that transparency you have to edit image yourself. Implement delegate method like this:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let cropRect = info[UIImagePickerControllerCropRect] as? CGRect {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
let imageRef: CGImage = image.cgImage!.cropping(to: cropRect)!
let image1: UIImage = UIImage(cgImage: imageRef, scale: image.scale, orientation: image.imageOrientation)
imageView.image = image1
picker.dismiss(animated: true, completion: nil)
}
}
}
Please try this code.
import MobileCoreServices //importing for mediaType kUTTypeImage
func showImagePicker() {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
let mediaTypes: Array<AnyObject> = [kUTTypeImage]
imagePicker.mediaTypes = mediaTypes as! [String]
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if picker.sourceType == UIImagePickerControllerSourceType.photoLibrary {
if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
dismiss(animated: true, completion: nil)
compImageView?.image = image
}
}
}

How to take picture automatically by programming in Swift?

I am developing in Swift with iPhone camera.
I using the following code to check the camera:
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = UIImagePickerControllerSourceType.Camera
picker.allowsEditing = true
self.presentViewController(picker, animated: true)
} else {
print("can't find camera")
}
}
And I use the following code to open the camera.
presentViewController(imagePicker, animated: true, completion: nil)
I have reference the following link and call takePicture() , but it seems did not working.
But I always need to manually take the picture by tapping the shutter button. Is programmatically taking the picture possible in Swift?
Thanks in advance.
1) Check if you have these delegates in your view controller class:
UIImagePickerControllerDelegate, UINavigationControllerDelegate
2) Check if your plist have the permissions with YES
Privacy - Photo Library Usage Description
Privacy - Camera Usage Description
If you want, I use this code (swift 3):
let imagePicker = UIImagePickerController()
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
self.present(imagePicker, animated: true, completion: nil)
For camera, just change "photoLibrary"
imagePicker.sourceType = .camera
3) In order to get the picture, you must implement the delegate didFinishPickingMediaWithInfo:
extension YourViewController:
UIImagePickerControllerDelegate,
UINavigationControllerDelegate
{
func imagePickerController(
_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : Any])
{
imagePicker.dismiss(animated: true, completion: nil)
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
print("picture taken: \(image)")
}
}
var imagePicker:UIImagePickerController = UIImagePickerController()
//Set delegate to imagePicker
imagePicker.delegate = self
//Allow user crop or not after take picture
imagePicker.allowsEditing = false
//set what you need to use "camera or photo library"
imagePicker.sourceType = .camera
//Switch flash camera
imagePicker.cameraFlashMode = .off
//Set camera Capture Mode photo or Video
imagePicker.cameraCaptureMode = .photo
//set camera front or rear
imagePicker.cameraDevice = .rear
//Present camera viewcontroller
self.present(imagePicker, animated: true, completion: nil)
and don't forget implement two protocol: UIImagePickerControllerDelegate,UINavigationControllerDelegate
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let imagePicked = info[UIImagePickerControllerOriginalImage] as? UIImage {
UIImageWriteToSavedPhotosAlbum(imagePicked, nil, nil, nil)
dismiss(animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
Because you want to take picture fully automatically, you don't need UIImagePickerController at all. Instead use AVFoundation directly.
This article will be very helpful: Camera Capture on iOS.

Swift - UIImage rotated 90 degrees when picked from UIImagePickerController Camera

I am using UIImagePickerController to pick images from both Camera and Library. When i get the image from the library the picture is OK. However, when i get the picture from the camera its rotating 90 degrees to the right. My app works just in portrait mode. Here is the code where i get the image
#IBAction func camPhoto(sender: AnyObject) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
#IBAction func importPhoto(sender: AnyObject) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
my Function importPhoto works fine but the other one doesn't.
func imagePickerController(picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let selectedPhoto = info[UIImagePickerControllerOriginalImage] as! UIImage
img.image = selectedPhoto
dismissViewControllerAnimated(true, completion: {
self.convBtn.hidden = false
})
}
Any ideas?
You can try like :
func imagePickerController(picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let selectedPhoto = info[UIImagePickerControllerOriginalImage] as! UIImage
**selectedPhoto.withHorizontallyFlippedOrientation()**
img.image = selectedPhoto
dismissViewControllerAnimated(true, completion: {
self.convBtn.hidden = false
})
}

Multiple Image Pickers in one Controller

I'm working on an onboarding flow for my app. I'm wanting to allow users to pick an avatar, display that avatar in an image view, and then select a background photo and display that in another image view. Currently, I have everything coded up, but for some reason when I present the picker controller, it defaults to controller for the avatar. I want the avatar button to only control the avatar button background image and the background button to only control the header image background image. Here's my code:
#IBAction func getBackground(sender: AnyObject) {
let backgroundPickerController = UIImagePickerController()
backgroundPickerController.delegate = self
backgroundPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
backgroundPickerController.allowsEditing = true
self.presentViewController(backgroundPickerController, animated: true, completion: nil)
}
#IBAction func selectAvatar(sender: AnyObject) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePickerController.allowsEditing = true
self.presentViewController(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// set image for button
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.addAvatar.setImage(image, forState: .Normal)
// Dismiss controller
self.dismissViewControllerAnimated(false, completion: nil)
}
func backgroundPickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
// set image for button
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.headerImage.image = image
self.dismissViewControllerAnimated(false, completion: nil)
}
The delegate function imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) also returns the UIImagePickerController that finished picking an image.
So you can use an if statement to check if the picker that finished is pickerOne or pickerTwo. Then you implement different behaviour according to the result of that.
Maybe set the pickers to nil after they have finished to clean up some memory.
class multiPickerVC : UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var pickerOne : UIImagePickerController?
var pickerTwo : UIImagePickerController?
override func viewDidLoad() {
//
}
#IBAction func getBackground(sender: AnyObject) {
pickerTwo = UIImagePickerController()
pickerTwo!.delegate = self
pickerTwo!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
pickerTwo!.allowsEditing = true
self.presentViewController(pickerTwo!, animated: true, completion: nil)
}
#IBAction func selectAvatar(sender: AnyObject) {
pickerOne = UIImagePickerController()
pickerOne!.delegate = self
pickerOne!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
pickerOne!.allowsEditing = true
self.presentViewController(pickerOne!, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
if picker == pickerOne {
// set image for button
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.addAvatar.setImage(image, forState: .Normal)
} else if picker == pickerTwo {
// set image for button
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.headerImage.image = image
}
self.dismissViewControllerAnimated(true, completion: nil)
}
}

UIImageView won't display chosen image from UIImagePicker

My imageView doesn't display the picture I choose with the UIImagePicker.
Here's all the code for it:
#IBAction func addImage(_ sender: Any) {
if !didShowCamera {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePicker.sourceType = .camera
imagePicker.cameraCaptureMode = .photo
} else {
imagePicker.sourceType = .photoLibrary
}
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
print(image)
self.jobImage.image = takenImage
self.takenImage = image
didShowCamera = true
self.dismiss(animated: true, completion: nil)
}
Any ideas how to solve it?
Use dispatch main queue
DispatchQueue.main.async {
self.jobImage.image = takenImage
self.takenImage = image
}
I guess the takenImage is nil while assigning to image view, you need to assign image to takenImage before setting to the image view. please update the method like,
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
print(image)
self.takenImage = image
self.jobImage.image = takenImage
didShowCamera = true
self.dismiss(animated: true, completion: nil)
}
Replace
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
with
let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
And check whether you have add privacy-photo library access in info.plist.
See detailed at [https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html#//apple_ref/doc/uid/TP40015214-CH6-SW1][1]

Resources