保存视频;使用UIImagePickerController; IOS 8 Swift

时间:2015-03-25 13:39:16

标签: ios swift uiimagepickercontroller

我是应用程序开发的新手,所以这可能是一个微不足道的问题,但请帮助!!

我正在尝试使用UIImagePickerController类将视频保存到相机胶卷。到目前为止,我已成功拉起相机并保存图像。我也能录制视频,但是当我按下"使用视频"它不会保存到相机胶卷。

下面是附加的didFinishPickingMediaWithInfo函数。

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject])
{
    let mediaType = info[UIImagePickerControllerMediaType] as NSString

    self.dismissViewControllerAnimated(true, completion: nil)

    if mediaType.isEqualToString(kUTTypeImage as NSString)
    {
        let image = info[UIImagePickerControllerOriginalImage] as UIImage

        if (newMedia == true)
        {
            UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil)
        }

        else if mediaType.isEqualToString(kUTTypeMovie as NSString)
        {
            let videoPath = info[UIImagePickerControllerMediaURL] as NSString

            if(newMedia == true)
            {
                UISaveVideoAtPathToSavedPhotosAlbum(videoPath, self,
                    "image:didFinishSavingWithError:contextInfo:", nil)
            }
        }
    }
}

这是useVideo例程。

@IBAction func useVideo(sender: AnyObject)
{
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
    {

        let imagePicker = UIImagePickerController()

        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
        imagePicker.mediaTypes = [kUTTypeMovie as NSString]
        imagePicker.allowsEditing = false

        self.presentViewController(imagePicker, animated: true, completion: nil)

        newMedia = true

    }
}

这是cameraRoll。

@IBAction func useCameraRoll(sender: AnyObject) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){

        let imagePicker = UIImagePickerController()

        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        imagePicker.mediaTypes = [kUTTypeImage as NSString]
        imagePicker.mediaTypes = [kUTTypeMovie as NSString]
        imagePicker.allowsEditing = false

        self.presentViewController(imagePicker, animated: true, completion: nil)

        newMedia = false
    }
}

2 个答案:

答案 0 :(得分:5)

如果关闭,你就会不匹配。

     if mediaType.isEqualToString(kUTTypeImage as NSString)
         {
             let image = info[UIImagePickerControllerOriginalImage] as UIImage

             if (newMedia == true)
             {
                 UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil)
             }

} //< - 此处关闭,如果条件为图片添加}

     else if mediaType.isEqualToString(kUTTypeMovie as NSString)
          {

答案 1 :(得分:0)

您可以使用此功能保存视频,希望有所帮助 -

public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
    let mediaType = info[UIImagePickerControllerMediaType] as! NSString

    if mediaType.isEqual(to: kUTTypeMovie as NSString as String)
    {
        // Is Video
        let url: URL = info[UIImagePickerControllerMediaURL] as! URL

        assetsLibrary.writeVideoAtPath(toSavedPhotosAlbum: url, completionBlock: {
            (nsurl, error) -> Void in
            if let theError = error{
                print("Error saving video = \(theError)")
                self.dismiss(animated: true, completion: nil)
            }
            else {
                print("no errors happened")
            }
        })
    }
}