Note The
Note The

Reputation: 1

How to make a function like a memo that can add a video from my library (I want it to be displayed in the text view)

I have tried the following ways(These) but none of them have been successful

How to make a function like a memo that can add a video from my library (I want it to be displayed in the text view)

(Below is my code)

extension DetailViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        videoURL = info[UIImagePickerController.InfoKey(rawValue: "UIImagePickerControllerReferenceURL") ] as? NSURL
        
        print(videoURL!)
        
        do {
            
            let asset = AVURLAsset(url: videoURL! as URL , options: nil)
            
            let imgGenerator = AVAssetImageGenerator(asset: asset)
            
            imgGenerator.appliesPreferredTrackTransform = true
            
            let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(value: 0, timescale: 1), actualTime: nil)
            
            let thumbnail = UIImage(cgImage: cgImage)
            
            selectedImage = thumbnail
            
        } catch let error {
            
            print("*** Error generating thumbnail: \(error.localizedDescription)")
       
        }
        
        if var selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
            
            let attachment = NSTextAttachment()
            
            if selectedImage.imageOrientation == .up || selectedImage.imageOrientation == .down {
                
                let img = selectedImage.resizeImage(image: selectedImage, targetSize: CGSize(width: self.view.frame.width - 50, height: self.view.frame.height))
                
                attachment.image = img.imageWithSpacing(insets: UIEdgeInsets(top: 15, left: 0, bottom: 15, right: 0))
                
            } else {
                
                let img = selectedImage.resizeImage(image: selectedImage, targetSize: CGSize(width: self.view.frame.width - 50, height: self.view.frame.height))
                
                attachment.image = img.imageWithSpacing(insets: UIEdgeInsets(top: 15, left: 0, bottom: 15, right: 0))
                
            }
            
            let attString = NSAttributedString(attachment: attachment)
            
            textView.textStorage.insert(attString, at: textView.selectedRange.location)
            
            picker.dismiss(animated: true, completion: nil)
            
        }
        
        let player = AVPlayer(url: videoURL! as URL)
        
        let playerController = AVPlayerViewController()
        
        playerController.player = player
        
        self.present(playerController, animated: true) {
            
            player.play()
            
        }
        
        let newPosition = textView.endOfDocument
        
        textView.selectedTextRange = textView.textRange(from: newPosition, to: newPosition)
        
        dismiss(animated: true, completion: nil)
        
    }
    
}

I want to know, how to put the selected video in the text view, and the position is correct, I am thinking, should I convert the videoURL into another format and put it in the NSTextAttachment, or is it really impossible to do so?

Upvotes: 0

Views: 60

Answers (0)

Related Questions