Search code examples
iosswiftinstagraminstagram-story

How to get NSData from image picker?


I try to share video to instagram stories from selected video from my photo library

func shareToInstagramStories(video: NSData) {
            // NOTE: you need a different custom URL scheme for Stories, instagram-stories, add it to your Info.plist!
            guard let instagramUrl = URL(string: "instagram-stories://share") else {
                return
            }

            if UIApplication.shared.canOpenURL(instagramUrl) {
                let pasterboardItems = [["com.instagram.sharedSticker.backgroundVideo": video as Any]]
                UIPasteboard.general.setItems(pasterboardItems)
                UIApplication.shared.open(instagramUrl)
            } else {
                //fail
            }
    }

here is the my image picker function

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        self.dismiss(animated: true, completion: nil)
        videoURL = info[UIImagePickerController.InfoKey.init(rawValue: "UIImagePickerControllerReferenceURL")] as? NSURL
     }

Solution

  • When I change the "UIImagePickerControllerReferenceURL" to "UIImagePickerControllerMediaURL" its totally worked

     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            self.dismiss(animated: true, completion: nil)
            videoURL = info[UIImagePickerController.InfoKey.init(rawValue: "UIImagePickerControllerMediaURL")] as? URL
     }