How to use "Use video" / "choose" button from video in swift 3? - ios

I'm trying to upload my video chosen from library or recorded from camera to firebase. When I get the video from library there's a "choose" button on the image picker or when I record the video it says "Use video".
I think to run those buttons function you have to write the didFinishPickingMediaWithInfo function. But nothing happens when I record a video and click "choose" this is what I have as my code:
if (UIImagePickerController.isSourceTypeAvailable(.camera)) {
if UIImagePickerController.availableCaptureModes(for: .rear) != nil {
//if the camera is available, and if the rear camera is available, the let the image picker do this
imagePicker.sourceType = .camera
imagePicker.mediaTypes = [kUTTypeMovie as String]
imagePicker.allowsEditing = false
imagePicker.delegate = self as? UIImagePickerControllerDelegate & UINavigationControllerDelegate
imagePicker.videoMaximumDuration = 60
imagePicker.videoQuality = .typeIFrame1280x720
present(imagePicker, animated: true, completion: nil)
} else {
postAlert("Rear camera doesn't exist", message: "Application cannot access the camera.")
}
} else {
postAlert("Camera inaccessable", message: "Application cannot access the camera.")
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("didFinishPickingMediaWithInfo")
}
I want it to at least print to know that the video "choose button" is working. Any ideas on how to get the choose button to work? Any help would be kind. Thank you in advance!

Related

How to to check image nil when access to camera and press cancel on camera

How to check image nil when access to camera and press cancel on camera
I’m tested
First step
Press take photo button
access camera
Press cancel I’m get value on output panel
Check image take photo ==> Optional(UIImagePickerController: 0x107879a00)
Press save button photo on project
I’m getting value nil
Fatal error: Unexpectedly found nil while unwrapping an Optional value 2019-07-05 08:52:24.667938+0700 MyProject[2307:786209] Fatal error: Unexpectedly found nil while unwrapping an Optional value
I’m tested
the second step
Take photo
2.press use photo on the ​camera
Take photo press use photo on camera
Check image take photo ==> Optional(UIImagePickerController: 0x10286ca00)
I want to check case cancel on camera and go back press save button on the project
How to fix….this case
My code below
Take photo
#IBAction func takePhotoReturnOfSealButton(_ sender: UIButton) {
imagePickerStoreListReturnSealLock = UIImagePickerController()
imagePickerStoreListReturnSealLock.delegate = self
imagePickerStoreListReturnSealLock.sourceType = .camera
present(imagePickerStoreListReturnSealLock,animated: true, completion: nil)
print("Check image take photo ==> \(String(describing: imagePickerStoreListReturnSealLock))")
}//takePhotoReturnOfSealButton
Save photo
#IBAction func saveImageToDevice(_ sender: UIButton) {
//Save photo to device not success have nil value
if(imagePickerStoreListReturnSealLock == nil){
showAlert(title: "No Have Photo", message: "Please take photo")
print("Check image cannot save ==> \(String(describing: imagePickerStoreListReturnSealLock))")
}else{
// Save photo to device success
UIImageWriteToSavedPhotosAlbum(showImageTakePhotoReturnOfSeal.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
print("Check image Save to device ==> \(String(describing: showImageTakePhotoReturnOfSeal))")
}
}//saveImageToDevice
Whenever you cancel the Image Picker a delegate method is been called -
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
//Perform your action when cancel has been pressed
}
This method is been called when Image is been picked -
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if picker == imagePicker {
//Save your image
imagePicker.dismiss(animated: true, completion: nil)
}
}
Just remember to define this above -
var imagePicker = UIImagePickerController()
and in viewDidLoad
imagePicker.delegate = self
When you click on cancel of image picker, following delegate method will be called:
public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
and when you select an image, Following delegate method will be called:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image : UIImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
picker.dismiss(animated: true, completion: nil)
UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)//to save image in saved photo album
}
Hope this helps

Error using UIImagePickerController under NavigationController and tabBarController

There are 3 UIViewControllers under NavigationController and tabBarController.
Push the viewControllers like this: A->B->C
In viewController C, add the following codes to let user to select image from photo library.
func btnClicked() {
let imagePickerController = UIImagePickerController()
imagePickerController.sourceType = .photoLibrary
imagePickerController.delegate = self
self.present(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage{
topIamge.image = image
}
picker.dismiss(animated: false, completion: nil)
}
Problem is that after the user select an image or cancel the selection, then press the left bar button going back to B, an error Unbalanced calls to begin/end appearance transitions for shows up, if continue going back to A, the error is printed out again.
Did I make anything wrong?
P.S. There is no error if I press the back button without presenting UIImagePickerController.
Thx.
I'm sure it doesn't related UIImagePickerController
Check these solution :
https://stackoverflow.com/a/12230777/6131436
https://stackoverflow.com/a/20925686/6131436

Generic - Creating an image format with an unknown type is an error - Swift 3

I have an image picker controller. I want to select an image from the photo library and place it in the rounded image view. The image gets placed successfully in the image view but still I get this error "[Generic] Creating an image format with an unknown type is an error".
I have seen many other questions for this but none of them solves my problem. All the questions say that didFinishPickingMediaWithInfo wasn't called but mine is not the case. My image is shown in the image view and I still get the error.
I have set the UIImagePickerControllerDelegate and UINavigationControllerDelegate. Picker is defined as follows:
let picker = UIImagePickerController()
Delegate is set in viewDidLoad as follows:
picker.delegate = self
Photo Library is opened as:
self.picker.allowsEditing = false
self.picker.sourceType = .photoLibrary
self.picker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
self.present(self.picker, animated: true, completion: nil)
My code of didFinishPickingMediaWithInfo is :
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
userImageView.image = image
} else{
print("Something went wrong")
}
self.dismiss(animated: true, completion: nil)
}
I am using xcode 8 and swift 3. Please help.

UIImagePickerController video recording starts after delay or not working

I am working on a application which needs custome camera UI. SO I used Overlay view in ImagePickerController. User can switch between capture modes.
(UIImagePickerControllerCameraCaptureMode.Video and UIImagePickerControllerCameraCaptureMode.Photo )
My problem is when user swich capture mode from .Photo to .Video startVideoCapture() function is taking time (up to 5 seconds) to initialize the video capturing at firs or second time.
Sometimes it does not save video on specified path..Photo mode is working fine.
At starting I initialized imagePickerController with .Photo mode. And user could switch .Photo to .Video. I thought may be initializing .Video mode would have taken time. For this I initialize the imagePickerController with .Video mode and set a timer to change .video to .Photo mode in 4 seconds. While these 4 seconds user cannot see camera as I have added Loading screen to stop user interaction.
But still problem persist. startVideoCapture() taking time to start recording for very first time. And sometimes it does not save video on specified path after recording.
Here is my code to save video in delegate method.
/**
* Delegate method for media capture and save media
*/
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info:[NSObject : AnyObject]) {
overlayView.bringSubviewToFront(preView)
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
if mediaType.isEqualToString(kUTTypeMovie as NSString as String){
let tempMedia = info[UIImagePickerControllerMediaURL] as! NSURL!
let pathString = tempMedia.relativePath
if pathString != nil {
destinationPathForVideo = "\(UtilityManager.sharedInstance.kcache)/challengeRecordings/\(NSDate()).mp4"
NSFileManager.defaultManager().createDirectoryAtPath(destinationPathForVideo.stringByDeletingLastPathComponent, withIntermediateDirectories: true, attributes: nil, error: nil)
UtilityManager.sharedInstance.copyFileFromSourceTo( pathString, destinationPath: destinationPathForVideo )
// Save Image
imageCaptured = getThumbnailForVideoPath(pathString!)
destinationPathForImage = "\(UtilityManager.sharedInstance.kcache)/challengeRecordings/\(NSDate()).png"
let success = UIImagePNGRepresentation(imageCaptured).writeToFile( destinationPathForImage , atomically: true)
Log(message:"Saved: \(success)")
imageButton!.setImage(imageCaptured, forState: .Normal)
imageView.image = imageCaptured
}
}
This is my initializing code
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera
imagePicker.showsCameraControls = false
self.imagePicker.mediaTypes = [kUTTypeMovie, kUTTypeImage]
self.imagePicker.videoMaximumDuration = 11
self.imagePicker.videoQuality = UIImagePickerControllerQualityType.TypeMedium
self.imagePicker.cameraFlashMode = .Off
self.imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video
Somebody please help.
I found a wonderful library with all mediapickercontroller features. You should try it. IQMediaPickerController

UIImagePicker - Do not allow panorama to be picked

Looking for help with an image picker. The picker is functioning properly - however - it allows the user to pick from Panorama photos as well as normal photos on their device.
I want to have the picker only allow the user to choose non panorama pictures.
I looked through the dev document and couldn't find anything about this. Any help appreciated! Here's my code:
#IBAction func selectImage(sender: AnyObject) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType =
UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.mediaTypes = [kUTTypeImage as NSString]
imagePicker.allowsEditing = false
self.clearBtn.hidden = false
self.presentViewController(imagePicker, animated: true,
completion: nil)
}
func imagePickerController(picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
self.dismissViewControllerAnimated(true, completion: nil)
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
self.postImage.contentMode = .ScaleAspectFit
self.postImage.image = image
self.hasPicture = true
}
I know that changing the UIImagePickerControllerSourceType to .PhotoLibrary will only allow picking from the camera roll, but I was hoping to still allow shared photo streams and such that just weren't panoramic.
As Christian posted in a comment - there is no native way for a UIImagePicker to only allow certain types of pictures (excluding panorama).
You have to handle validating the image after it is returned.

Resources