Swift - 如何在iPhone上获取所有照片和视频的列表?

时间:2015-04-18 21:54:22

标签: ios swift video uiimagepickercontroller photo

我希望能够查看存储在用户iPhone上的照片和视频列表,以便我们允许他们选择要上传的文件。到目前为止,我已经将照片显示在列表中,但没有视频出现。我用来显示照片库的代码如下:

@IBAction func btnAddPicOrVideo(sender: AnyObject) {

    let pickerC = UIImagePickerController()        
    pickerC.delegate = self        
    self.presentViewController(pickerC, animated: true, completion: nil)

}

正如我所提到的,我能够显示照片列表并选择其中一张照片。问题是我无法看到或选择任何视频。有没有办法指定要显示的图片和视频?或者,我是否必须单独显示图片和视频?

我目前正在模拟器上运行我的代码,并且我在本地存储了一个视频文件。

提前致谢。

2 个答案:

答案 0 :(得分:0)

我能够通过指定

来解决这个问题
import MobileCoreServices

我改变了上面指定的代码:

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

}

答案 1 :(得分:0)

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



}
相关问题