3

I am having trouble completely removing an image and all the "meta data" from the NSAttachMent in the NSMutableAttributedString. To give a high level explanation of my scenario - a user can upload an image or text to a social media feed. I use UITextView for the input and display for both the text and image. So my thought is if the user wants to insert a message and an image (vice versa too), I use the following code below:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    let image = info[UIImagePickerControllerOriginalImage] as? UIImage

    imagePicker?.dismissViewControllerAnimated(true, completion: nil)

    let mutableAttrString = NSMutableAttributedString()

    let attachment = NSTextAttachment()

    var width : CGFloat?
    var height : CGFloat?

    let size = CGSizeMake(width!, height!)

    let imgToDisplay = scaleImageWithoutChangingAspect(image!, toSize: size)

    attachment.image = imgToDisplay

    mutableAttrString.appendAttributedString(format.prepAtrributedString(textToStore + "\n", f: UIFont(name: fontRegular, size: 20)!, Color: UIColor.blackColor()))

    let attString = NSAttributedString(attachment: attachment)

    mutableAttrString.appendAttributedString(attString)
    //add this attributed string to the current position.

    self.TextMessage.textStorage.insertAttributedString(mutableAttrString, atIndex: self.TextMessage.selectedRange.location)
    self.TextMessage.attributedText = mutableAttrString
    self.TextMessage.invalidateIntrinsicContentSize()
    self.TextMessage.updateConstraintsIfNeeded()
    self.TextMessage.layoutIfNeeded()
}

Here is the code I use to attempt to remove the image from the attributed text, however after the removal I see a unicode character \u{ef}:

let s = self.TextMessage.text

self.TextMessage.resignFirstResponder()

print(s)


let mutAttrString = NSMutableAttributedString(attributedString: self.TextMessage.attributedText)

let range = NSMakeRange(0, mutAttrString.length)

mutAttrString.enumerateAttributesInRange(range, options: NSAttributedStringEnumerationOptions.Reverse) { (OBJS, Range, B) -> Void in
    for s in OBJS {
        if s.0 == "NSAttachment" {
            mutAttrString.removeAttribute(s.0, range: Range)
        }
    }
}


mutAttrString.endEditing()

var insertMessageData = mutAttrString.string

let db = SwiftDatabase()

var ImgData : NSData?

if let i = selectedImage {
    ImgData =   UIImageJPEGRepresentation(i, 0.5)
}

///Insert text and upload image to database.
let tup =  db.insertShowMessage(insertMessageData, data: ImgData,delegate: self)
key_id = tup.KeyId
query = tup.Query

hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)

I also tried converting the text to ASCII, but I still see the "Meta data" NSTextAttachMent. Instead of an unicode character, I see a "?."

I would greatly appreciate any help. Thanks!

Bista
  • 7,869
  • 3
  • 27
  • 55
user3766198
  • 109
  • 11
  • 1
    Don't remove the attribute, remove that part of the string with this: http://stackoverflow.com/questions/37951812/delete-remove-nstextattachment-from-uitextview/37952099#37952099 – Larme Aug 26 '16 at 14:40
  • You're right! Thanks! I'll answer this questions when stackoverflow lets me. – user3766198 Aug 26 '16 at 17:16
  • Mark as duplicate if it's the same. – Larme Aug 26 '16 at 17:18

0 Answers0