Getting video using UIImagePickerController - ios

I'm trying to get a selected video from an iOS app using UIImagePickerController, and so far when the button is clicked it opens the PhotoLibrary perfectly fine and I select a video and it says compressing video, so it's acting like its selecting it but after that nothing really happens. And I can't really seem to figure out why.
var picker = UIImagePickerController()
var imag = UIImagePickerController()
...
#IBAction func selectMediaAction(sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
picker.delegate = self
picker.allowsEditing = false
picker.mediaTypes = [kUTTypeMovie as String]
self.presentViewController(picker, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
let media = editingInfo[UIImagePickerControllerOriginalImage]
let imageURL = media?.filePathURL as NSURL?
mediaPath = imageURL
mediaName.text = imageURL!.lastPathComponent
self.dismissViewControllerAnimated(true, completion: nil)
toggleSubmit()
}
#IBAction func createMediaAction(sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
imag.delegate = self
imag.allowsEditing = false
imag.sourceType = UIImagePickerControllerSourceType.Camera
imag.mediaTypes = [kUTTypeMovie as String]
self.presentViewController(imag, animated: true, completion: nil)
} else{
let alert = UIAlertController(title: nil, message: "There is no camera available on this device", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
Am I missing something? It looks to me like it should be working

The code you have will only check for the image types. If you want to get the selected video information, you need to look for the key UIImagePickerControllerMediaType and you need to use the delegate method didFinishPickingMediaWithInfo not the didFinishPickingImage
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];

Related

How display picture in a IOS app?

I'm a beginner to build an IOS app and I want to know the good practice to display user picture on my app. I searched on google if some practice or document talk about that but i found nothing... My question mark is:
What is the common ratio to use when we want a "portrait picture" or "selfie"?
For my app I need to display a fixed ratio image and I would like authorize the user to crop the image, so do you know a library to help me to do that?
Thanks for your reply
this is what i'm using :
displayPhotoImageView is uiimageView photo loads on it
var newpic:Bool?
func photoAction(){
let myalert=UIAlertController(title:"",message:"choose photo ",preferredStyle: .actionSheet)
let camerAction=UIAlertAction(title:"camera",style:.default){(action) in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagepicker = UIImagePickerController()
imagepicker.delegate = self
imagepicker.sourceType = UIImagePickerControllerSourceType.camera
imagepicker.mediaTypes = [kUTTypeImage as! String]
imagepicker.allowsEditing = false
self.present(imagepicker, animated: true, completion: nil)
self.newpic = true
}
}
// let camerRollAction=UIAlertController(title:"",message:"",preferredStyle: .actionSheet)
let camerRollAction=UIAlertAction(title:"Gallery",style:.default){(action) in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
let imagepicker = UIImagePickerController()
imagepicker.delegate = self
imagepicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagepicker.mediaTypes = [kUTTypeImage as! String]
imagepicker.allowsEditing = false
self.present(imagepicker, animated: true, completion: nil)
self.newpic = true
}
}
myalert.addAction(camerAction)
myalert.addAction(camerRollAction)
self.present(myalert, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let mediatype = info[UIImagePickerControllerMediaType] as! NSString
if mediatype.isEqual(to: kUTTypeImage as! String) {
let image=info[UIImagePickerControllerOriginalImage] as! UIImage
displayPhotoImageView.image = image
let imageData:NSData = UIImagePNGRepresentation(image)! as NSData
data = imageData.base64EncodedString(options: .lineLength64Characters)
if newpic == true {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(imageError), nil)
}
}
self.dismiss(animated: true, completion: nil)
}
func imageError(image:UIImage,didFinishSavingWithError error:NSErrorPointer,ContextInfo:UnsafeRawPointer){
if error != nil {
let alert=UIAlertController(title: "error ", message: " unable ", preferredStyle: .alert)
let ok=UIAlertAction(title: "ok", style: .cancel, handler:nil )
alert.addAction(ok)
self.present(alert, animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
you need Privacy - Camera Usage Description and Privacy - Photo Library Usage Description permission

Both UIImage displaying different image [duplicate]

This question already has an answer here:
Picking two different images in the same view controller using imagePickerController in Swift
(1 answer)
Closed 4 years ago.
I am adding 2 function UITapGestureRecognizer for my 2 UIImage for displaying diffrent picture each UIImage.
So when I tap one of my UIImage, UIAlertController will show and give 2 option for adding a picture (using Camera or using Library)
But when I'm adding the picture, both of them is displaying same picture (when I using imagePickerController
and after I add if else statement, the picture is only displayed on if statement even when I tap on the second UIImage
For example:
I've 2 UIImage named imageView1.image and imageView2.image
I tap on my first imageView1.image, UIAlertController does show option (camera and library)
I choose library and select image1.jpg
After i select image1.jpg, both imageView1.image and imageView2.image displaying same picture.
After I'm adding if else statement in the func imagePickerController, the image1.jpg is only displayed on the if statement... Even i tap on the imageVIew2.image
But, what I want is...
When i tap imageView1.image and select image1.jpg, imageView1.image displaying image1.jpg and imageView2.jpg isnt displaying anything.
Here's my code (without if else):
extension KUYPEP {
//pencetGambarnya1 for imageView1.image
#objc func pencetGambarnya1(tapGestureRecognizer1: UITapGestureRecognizer) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
let gambarDipencet1 = tapGestureRecognizer1.view as! UIImageView
let act1 = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
act1.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePickerController.sourceType = .camera;
self.present (imagePickerController, animated: true, completion: nil)
} else {
print ("Camera ruksak")
}
}))
act1.addAction(UIAlertAction(title: "Library", style: .default, handler: {(UIAlertAction) in
imagePickerController.sourceType = .photoLibrary
self.present(imagePickerController, animated: true, completion: nil)
}))
act1.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: {(UIAlertAction)in}))
self.present(act1, animated: true, completion: nil)
print ("berhasil11")
}
//pencetGambarnya2 for imageView2.image
#objc func pencetGambarnya2(tapGestureRecognizer2: UITapGestureRecognizer) {
let imagePickerController2 = UIImagePickerController()
imagePickerController2.delegate = self
let gambarDipencet2 = tapGestureRecognizer2.view as! UIImageView
let act2 = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
act2.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePickerController2.sourceType = .camera;
self.present (imagePickerController2, animated: true, completion: nil)
} else {
print ("Camera ruksak")
}
}))
act2.addAction(UIAlertAction(title: "Library", style: .default, handler: {(UIAlertAction) in
imagePickerController2.sourceType = .photoLibrary
self.present(imagePickerController2, animated: true, completion: nil)
}))
act2.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: {(UIAlertAction)in}))
self.present(act2, animated: true, completion: nil)
print ("berhasil22")
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image1 = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView1.image = image1
let image2 = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView2.image = image2
picker.dismiss(animated: true, completion: nil)
print ("Slavic Hardbass")
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
}
And here's my viewDidLoad
let tapGestureRecognizer1 = UITapGestureRecognizer(target: self, action: #selector(pencetGambarnya1(tapGestureRecognizer1:)))
imageView1.isUserInteractionEnabled = true
imageView1.addGestureRecognizer(tapGestureRecognizer1)
let tapGestureRecognizer2 = UITapGestureRecognizer(target: self, action: #selector(pencetGambarnya2(tapGestureRecognizer2:)))
imageView2.isUserInteractionEnabled = true
imageView2.addGestureRecognizer(tapGestureRecognizer2)
Create one public UIImage named targetImage and do like the following,
assign the reference of image1 or image2 to targetImage just before presenting imagePickerController. When finished picking the image, just set the picked image to targetImage. You can see in the following code, i have set targetImage = imageView1.
act1.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePickerController.sourceType = .camera;
targetImage = imageView1
self.present (imagePickerController, animated: true, completion: nil)
} else {
print ("Camera ruksak")
}
}))
And do the same for imageView2 as well,
After that modify code to set picked media like this,
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image1 = info[UIImagePickerControllerOriginalImage] as! UIImage
targetImage.image = image1
picker.dismiss(animated: true, completion: nil)
print ("Slavic Hardbass")
}
I have not tested this, but this should work.
Try this Code. Hope it works for you
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if picker == imagePickerController
{
let image1 = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView1.image = image1
}
else
{
let image2 = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView2.image = image2
}
picker.dismiss(animated: true, completion: nil)
print ("Slavic Hardbass")
}
Solve it by adding var in the class using this code
answer
Thank you for replying.

swift ios when do I need to use mediaTypes kUTTypeImage

I have a image picker in my app where you can select images from camera roll or take a new picture then upload the images to my backend server.
But looking around others code I see that some people use this:
imagePickerController.mediaTypes = [kUTTypeImage as String]
Why do you need to set mediaTypes to kUTTypeImage?
I have not used that in my code bellow but everything still works fine
I select images like this through a UIAlertController:
//Check if camera exist
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
let cameraAction = UIAlertAction(title: "Take a photo", style: .Default) { (action) in
self.imagePicker.sourceType = .Camera
self.imagePicked = 1
self.presentViewController(self.imagePicker, animated: true, completion: nil)
}
alertController.addAction(cameraAction)
}
//Check if photo lib exist
if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary) {
let photosLibraryAction = UIAlertAction(title: "Pick image", style: .Default) { (action) in
self.imagePicker.sourceType = .PhotoLibrary
self.imagePicked = 1
self.presentViewController(self.imagePicker, animated: true, completion: nil)
}
alertController.addAction(photosLibraryAction)
}
Then I get the images:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
//Change aspect when not dummy image
self.bigImage.contentMode = .ScaleAspectFill
self.bigImage.clipsToBounds = true
self.bigImage.image = pickedImage
kUTTypeImage is actually default for the mediaTypes property. It states, that one can only pick still images. If you are ok with this default, you don't need to set it explicitly in your code.
Here is the documentation: https://developer.apple.com/reference/uikit/uiimagepickercontroller/1619173-mediatypes

My imagePickerController didFinishPickingMediaWithInfo newer get called

I try to write an App that needs a screen where you can take multible photos. I have used a code example from http://makeapppie.com/2015/11/04/how-to-make-xib-based-custom-uiimagepickercontroller-cameras-in-swift/.
It seems to be working OK, but my imagePickerController didFinishPickingMediaWithInfo newer get called. I am getting an error message from Xcode "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." It sounds to me like this could be the problem, and I have googled it, but havn't gotten any wiser. A lot of people write it's an Apple bug and I havn't found anybody offering a solution.
So do anybody know if it is the Xcode error that is my problem, and in that case have a solution for that or have I written something wrong in my code:
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, CustomOverlayDelegate {
var picker = UIImagePickerController()
#IBAction func shootPhoto(sender: AnyObject) {
if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
picker = UIImagePickerController() //make a clean controller
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.Camera
picker.cameraCaptureMode = .Photo
picker.showsCameraControls = false
//customView stuff
let customViewController = CustomOverlayViewController(
nibName:"CustomOverlayViewController",
bundle: nil
)
let customView:CustomOverlayView = customViewController.view as! CustomOverlayView
customView.frame = self.picker.view.frame
customView.cameraLabel.text = "Hello Cute Camera"
customView.delegate = self
//presentation of the camera
picker.modalPresentationStyle = .FullScreen
presentViewController(picker, animated: true,completion: {
self.picker.cameraOverlayView = customView
})
} else { //no camera found -- alert the user.
let alertVC = UIAlertController(
title: "No Camera",
message: "Sorry, this device has no camera",
preferredStyle: .Alert)
let okAction = UIAlertAction(
title: "OK",
style:.Default,
handler: nil)
alertVC.addAction(okAction)
presentViewController(
alertVC,
animated: true,
completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
print("didFinishPickingMediaWithInfo")
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //get the image from info
UIImageWriteToSavedPhotosAlbum(chosenImage, self,nil, nil) //save to the photo library
}
//What to do if the image picker cancels.
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
//MARK: Custom View Delegates
func didCancel(overlayView:CustomOverlayView) {
picker.dismissViewControllerAnimated(true, completion: nil)
print("dismissed!!")
}
func didShoot(overlayView:CustomOverlayView) {
picker.takePicture()
overlayView.cameraLabel.text = "Shot Photo"
print("Shot Photo")
}
func weAreDone(overlayView: CustomOverlayView) {
picker.dismissViewControllerAnimated(true,
completion: nil)
print("We are done!")
}
}
Write
picker.delegate = self after
picker = UIImagePickerController() line
Also inherit you class with UIImagePickerControllerDelegate delegate.
It will work.

Swift - How can I get a list of all photos and videos on iPhone?

I would like to be able to view a list of both photos and videos stored on the user's iPhone so I can allow them to select the file for upload. So far, I have it working where photos show up in the list, but no videos are showing up. The code I'm using to display the photos library is the following:
#IBAction func btnAddPicOrVideo(sender: AnyObject) {
let pickerC = UIImagePickerController()
pickerC.delegate = self
self.presentViewController(pickerC, animated: true, completion: nil)
}
As I mentioned, I'm able to display a list of photos and select one of them just fine. The problem is that I'm unable to see or select any videos. Is there a way to specify for both pictures and videos to be displayed? Or, do I have to display pictures and videos separately?
I'm currently running my code on the simulator and I have a video file stored on it locally.
Thanks in advance.
I was able to get this resolved by specifying
import MobileCoreServices
and I changed the code I specified above as such:
#IBAction func btnAddPicOrVideo(sender: AnyObject) {
let pickerC = UIImagePickerController()
pickerC.mediaTypes = [kUTTypeImage as NSString, kUTTypeMovie as NSString]
pickerC.delegate = self
self.presentViewController(pickerC, animated: true, completion: nil)
}
class ScoutDetailPage: UIViewController,UIImagePickerControllerDelegate {
var picker:UIImagePickerController? = UIImagePickerController()
let imageView = UIImageView ()
{
override func viewDidLoad(){
// Do any additional setup after loading the view.
self.loadOrTakePhotos()
}
func loadOrTakePhotos()
{
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
{
picker!.sourceType = UIImagePickerControllerSourceType.Camera
picker?.delegate = self
self .presentViewController(picker!, animated: true, completion: nil)
}
}
else if (pickersegment.selectedSegmentIndex == 1)
{
picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
picker?.delegate = self
self.presentViewController(picker!, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let pickedimage = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView.image = pickedimage
if (imageView.image != nil)
{
print("image not empty")
// Do something here.
picker .dismissViewControllerAnimated(false, completion: nil)
}
else
{
print("IMAGE VIEW NIL")
}
}
func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafePointer<Void>) {
if error != nil {
let alert = UIAlertController(title: "Save Failed",
message: "Failed to save image",
preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "OK",
style: .Cancel, handler: nil)
alert.addAction(cancelAction)
self.presentViewController(alert, animated: true,
completion: nil)
}
}
}

Resources