2

I have this code, I read image from Library and I want to resize the image in uitextview with dragging it (in real time ). Does anyone know how can I do this?

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
            let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage

let attachment = NSTextAttachment()
        attachment.image = pickedImage
        //calculate new size.  (-10 because I want to have a litle space on the right of picture)
        let newImageWidth = (txtEditor.bounds.size.width - 10 )
        let scale = newImageWidth/(pickedImage?.size.width)!
        let newImageHeight = (pickedImage?.size.height)! * scale
        //resize this
        attachment.bounds = CGRect.init(x: 0, y: 0, width: newImageWidth, height: newImageHeight)
        //put your NSTextAttachment into and attributedString
        let attString = NSAttributedString(attachment: attachment)
        //add this attributed string to the current position.
        txtEditor.textStorage.insert(attString, at: txtEditor.selectedRange.location)
}
zero323
  • 322,348
  • 103
  • 959
  • 935
Miloo
  • 117
  • 1
  • 2
  • 10

0 Answers0