简体   繁体   English

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

[英]saving video; using UIImagePickerController; IOS 8 Swift

I'm new to app developing so this may be a trivial question but please HELP!! 我是应用开发的新手,所以这可能是一个琐碎的问题,但请帮助!!

I am trying to save a video to the Camera Roll using the UIImagePickerController class. 我正在尝试使用UIImagePickerController类将视频保存到相机胶卷。 So far I have been successful pulling up the camera and saving an image. 到目前为止,我已经成功地拉起相机并保存了图像。 I have also been able to record video, but when I press "use video" it does not save to the camera roll. 我也可以录制视频,但是当我按“使用视频”时,它不会保存到相机胶卷中。

Below is the attached didFinishPickingMediaWithInfo function. 下面是附加的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)
            }
        }
    }
}

Here is the useVideo routine. 这是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

    }
}

Here is the cameraRoll. 这是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
    }
}

You have mismatched if close. 如果相近,则您不匹配。

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

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

} // <--here you close if condition for image add } } // <-如果添加图像条件,此处关闭}

     else if mediaType.isEqualToString(kUTTypeMovie as NSString)
          {

You can save video using this, hope it helps - 您可以使用此功能保存视频,希望对您有所帮助-

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM