Search code examples
iosswiftattributesnsattributedstringnstextattachment

export image from attributed text in Swift


I have an attributed text which contains an NSTextAttachment which I insert earlier. Now I need to extract that image and any text to individual variables to save them separately. But I get an error which provides me with no more information other that EXC_BAD_ACCESS in the first block. What am I missing? any information would be helpful. Thank you in advance.

 if composeView.textView.attributedText == nil{
        println("no attributed text")
    }else{
        let range:NSRange = NSMakeRange(0, composeView.textView.attributedText.length)
        self.composeView.textView.attributedText.enumerateAttributesInRange(range, options: nil, usingBlock: { (dic: [NSObject:AnyObject]?, rang:NSRange!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in


           var attachement = dic as NSDictionary!
            attachement.enumerateKeysAndObjectsUsingBlock({ (key:AnyObject?, obj:AnyObject?, stop:UnsafeMutablePointer<ObjCBool>) -> Void in


                var stri = key as NSString!
                println(stri)
                if stri == "NSAttachment"{
                    var att:NSTextAttachment = obj as NSTextAttachment
                    if att.image == nil{
                        println("no image")
                        self.saveMessageToParse(text, image: nil)
                    }else{
                        var im:UIImage = att.image!
                        println("image exists")

                        self.saveMessageToParse(text, image: im)
                    }

                }


            })

        })

Solution

  • problem was solved by itself after updating to Xcode 6.3... the actual code is working great for anyone who wants to use it.