2

I have a function for handling pasted images and I'm trying to handle them with my server like iMessage does: having separate messages for text before and after pasted images.

This is my code for adding the image to the textView.

override func didPasteMediaContent(userInfo: [NSObject : AnyObject]) {
    if let pickedImage = UIPasteboard.generalPasteboard().image {
        // Image Caching
        if let data = UIImageJPEGRepresentation(pickedImage, 0.50) {
            hanekeCount += 1
            haneke.set(value: data, key: "pastedImage\(hanekeCount)")
        }

        // Image resizing
        let textViewWidth: CGFloat = self.textView.frame.size.width - 20
        let percentResize = textViewWidth / pickedImage.size.width
        let toBeExportedHeight = pickedImage.size.height * percentResize
        let resizedImage = ImageManipulationManager.sharedInstance.resizeImage(exportedWidth: Int(textViewWidth),exportedHeight: Int(toBeExportedHeight), originalImage: pickedImage)

        // Storage into TextView
        let attributedString = NSAttributedString(string: textView.text)
        let attachment = NSTextAttachment()
        attachment.image = resizedImage
        let attString = NSAttributedString(attachment: attachment)
        print(textView.selectedRange.location)
        textView.textStorage.insertAttributedString(attString, atIndex: textView.selectedRange.location)
        textView.selectedRange.location = textView.selectedRange.location + 1
        textView.textStorage.insertAttributedString(NSAttributedString(string: "\n"), atIndex: textView.selectedRange.location)
        textView.selectedRange.location = textView.selectedRange.location + 1
        textView.font = UIFont.systemFontOfSize(16.0)
    }
}
  • 1
    You mean like this: http://stackoverflow.com/questions/29152660/extract-uiimage-from-nsattributed-string/29153172 ? – Larme Aug 21 '16 at 21:10

0 Answers0