Swift - How can I get a list of all photos and vid

2019-08-05 08:01发布

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.

2条回答
混吃等死
2楼-- · 2019-08-05 08:30

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)

}
查看更多
孤傲高冷的网名
3楼-- · 2019-08-05 08:35
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)
        }
    }



}
查看更多
登录 后发表回答