Crop square photo with picker.allowsEditing = true in Swift - ios

I'm currently coding a social app which permits to share pictures. The user has the possibility to share pictures from his/her library.
func selectImg() {
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .photoLibrary
picker.mediaTypes = ["public.image"]
picker.allowsEditing = true
present(picker, animated: true, completion: nil)
}
Then, the user has to crop the picture in order to having a square version of it. For that, I wrote picker.allowsEditing = true, but the problem is that the cropped zone is not seems to be improper, here is what is looks like:
As you can see with this square picture, the cropping zone is displaced and I really don't know why. I tried to check if users had the same problem, but no result.

Related

Preselecting images with UIImagePickerController

I have a standard code to open image picker, to select multiple images with it:
private func openFilesAction() {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let imagePicker = UIImagePickerController()
customizePicker(picker: imagePicker)
imagePicker.delegate = self
imagePicker.mediaTypes = ["public.movie"]
imagePicker.sourceType = .photoLibrary;
imagePicker.allowsEditing = false
imagePicker.videoExportPreset = AVAssetExportPresetPassthrough
imagePicker.videoQuality = .typeHigh
self.present(imagePicker, animated: true, completion: nil)
}
}
But, I would like to have an ability to open picker again, and add or remove pictures to current selection. So the next time I open a picker, previously chosen images to be preselected.
Is this possible with a default picker, or I will have to do it custom (using CollectionView)?
You are not able to use multiple selection and preselection using UIImagePickerController

How to have a choose image button with a preview of last taken image

I am building a camera app and would like to have the last taken photo show up as a button to choose a photo. How would I do that?
Right now I am using UIImagePickerController to choose the image, and UIButton with just text to bring up the controller.
#IBAction func choosePhotoBtnPressed(_ sender: Any) {
let imagePicker = UIImagePickerController()
imagePicker.sourceType = .photoLibrary
imagePicker.mediaTypes = [kUTTypeImage as String, kUTTypeMovie as String]
imagePicker.delegate = self
present(imagePicker, animated: true, completion: nil)
}
I want it to look like the stock camera app.
https://i1.wp.com/9to5mac.com/wp-content/uploads/sites/6/2013/09/screen-shot-2013-09-20-at-9-56-50-am.png?resize=1600%2C1000&quality=82&strip=all

UIImagePickerController back button/cancel button is not visible

I show the phone's gallery with an UIImagePickerController, but there is no back/cancel button on it. How can I add one?
What I have (inside a UIViewController):
func openGallery() {
let picker = UIImagePickerController()
picker.allowsEditing = false
picker.sourceType = .photoLibrary
picker.mediaTypes =
UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
present(picker, animated: true, completion: nil)
}
When I run this, I get the gallery, however there is no cancel button at top where it usually is.
Implement UIImagePickerControllerDelegate , UINavigationControllerDelegate and create a variable to assign UIImagePickerController var imagePicker = UIImagePickerController()
and set imagePicker.delegate = self.
func openGallary()
{
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}

How to add few options to UIImagePickerController

There is way to add few options to imagePicker sourceType?
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
The options is specific to the Camera app and is not available through the API.
You should file an enhancement request at http://bugreport.apple.com.
imagePicker.showsCameraControls = true. // use this
imagePicker.modalPresentationStyle = .FullScreen
If you make your own custom than you can create custom view
imagePicker.cameraOverlayView = YourView

UIImagePickerController slow on opening for the first time, except for when you double tap

I'm getting this really weird behaviour on iOS 9 with swift where I have a tableViewCell that opens an imagePicker when tapped on to take a picture of something, when you tap the cell for the first time it takes like 10 seconds to open the picker, but when you tap it twice it immediately opens...
The initialisation code for the picker is as follows
let certificateImagePicker = UIImagePickerController()
certificateImagePicker.delegate = self
certificateImagePicker.allowsEditing = false
certificateImagePicker.sourceType = .Camera
certificateImagePicker.modalPresentationStyle = .CurrentContext
The code for presenting the picker is presentViewController(certificateImagePicker, animated: false, completion: nil)
I do not now if it related but after opening the picker it show this error message
Snapshotting a view that has not been rendered results in an empty snapshot.
Ensure your view has been rendered at least once before snapshotting or
snapshot after screen updates.
I experienced a similar delay in presenting a UIImagePickerController on the first try. What helped a lot in my case was initialising it when also initializing the parent UIViewController, like so:
class ExampleViewController: UIViewController, UIImagePickerControllerDelegate {
let imagePicker = UIImagePickerController()
func presentImagePicker() {
imagePicker.delegate = self
imagePicker.allowsEditing = false
imagePicker.sourceType = .camera
imagePicker.modalPresentationStyle = .currentContext
self.present(imagePicker, animated: false, completion: nil)
}
}
This is only for debug builds, when you run an app connected to Xcode.
private var imagePickerController = UIImagePickerController()
...
#objc func addPhoto() {
imagePickerController.delegate = self
imagePickerController.sourceType = .savedPhotosAlbum
present(imagePickerController, animated: true)
}
...
When I have this implementation it was waiting when the first present was attempted then after changed my variable to lazy variable, then it worked like charm.
private lazy var imagePickerController = UIImagePickerController()

Resources