Swift-如何获取iPhone上所有照片和视频的列表? [英] Swift - How can I get a list of all photos and videos on iPhone?

查看:395
本文介绍了Swift-如何获取iPhone上所有照片和视频的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够查看用户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.

先谢谢了.

推荐答案

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)
        }
    }



}

这篇关于Swift-如何获取iPhone上所有照片和视频的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆