简体   繁体   English

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

[英]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. 我希望能够查看用户iPhone上存储的照片和视频的列表,以便允许他们选择要上传的文件。 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)
        }
    }



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Photo Framework将iPhone中存储的所有照片获取到NSArray? - How to get all the photos stored in iPhone to NSArray using Photo Framework? 如何从照片库中检索数组中的所有照片和视频? - How to retrieve all the photos and videos in an array from Photos library? 我可以从iPhone中的照片中获取图像的名称吗? - Can i get the name of image from the photos in iPhone Swift-如何获取包含所有照片的相册包括iCloud库? - Swift - How get photo albums with all photos include iCloud Library? iPhone/iOS:如何获取我的应用本地化的所有语言的本地化字符串列表? - iPhone/iOS: How can I get a list of localized strings in all the languages my app is localized in? 如何在Swift中获取iPhone的当前俯仰,偏航和侧倾? - How can I get the current pitch, yaw and roll of the iPhone in Swift? 如何实现iPhone Photos编辑抽屉? - How can I implement the iPhone Photos editing drawer? 如何使用 Swift 从图库中选择多个视频和照片 - How to pick multiple videos as well as photos from gallery using Swift 使用iOS Photos Framework,如何列出所有可用的PHAssetCollections? - With the iOS Photos Framework how do I list all PHAssetCollections available? 如何将相机胶卷中的所有照片和视频插入阵列? - How to insert all photos and videos from camera roll into array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM