如何在swift 3中将图像和视频上传到服务器

时间:2017-11-07 22:34:25

标签: ios iphone swift3

我使用此源代码上传图片,但视频无法成功上传。我必须更改上传视频的源代码。请帮助我。在这里,我分享了我的源代码。

#

请帮我解决这个问题。我是iOS Swift的新手。或者请发送任何相关链接以成功上传图像和视频

1 个答案:

答案 0 :(得分:0)

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
    {
        print(info)
        if info[UIImagePickerControllerMediaType] as? String == "public.image"
        {
            if let imagedata = info[UIImagePickerControllerOriginalImage] as? UIImage        {

                PostImageView = imagedata
                btnphoto.setImage(imagedata, for: .normal)

            }

            dismiss(animated: true, completion: nil)
        }
        else
        {
            if let url  = info[UIImagePickerControllerMediaURL] as? URL
            {
                SelectUrl = url
                dismiss(animated: true, completion: nil)
                play(url: SelectUrl!)

            }

        }
    }

    func play(url:URL)
    {
        let player = AVPlayer(url: url)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player
        self.present(playerViewController, animated: true)
        {
            playerViewController.player!.play()
        }

        do {
            let asset = AVURLAsset(url: url , options: nil)
            let imgGenerator = AVAssetImageGenerator(asset: asset)
            imgGenerator.appliesPreferredTrackTransform = true
            let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(0, 1), actualTime: nil)
            thumbnail = UIImage(cgImage: cgImage)

           btnVideo.setImage(thumbnail, for: .normal)



        } catch let error {
            print("*** Error generating thumbnail: \(error.localizedDescription)")
        }

    }

在服务代码中

 if self.thumbnail != nil
                        {

                            dicParameter.setObject(self.thumbnail!, forKey: "video_thumb" as NSCopying)


                            let fileData = Utilities.getDataFrom(url: self.SelectUrl)

                            if fileData != nil {
                                var fileParameter : [String : Any] = [:]
                                fileParameter[FileParameter.fileName] = "video.mov"
                                fileParameter[FileParameter.fileData] = fileData
                                fileParameter[FileParameter.fileKey] = "post_file"
                                dicParameter.setObject(fileParameter, forKey: "post_file" as NSCopying)
                            }


                        }
                        else
                        {

                            let imageData = UIImagePNGRepresentation(self.PostImageView! )
                            if imageData==nil { print("image data is nil"); return }
                            dicParameter.setObject(self.PostImageView, forKey: "post_file" as NSCopying)

                        }