Extract UIImage from NSAttributed string

What am I doing:

  • NSATTRIBUTE STRING = NSSTRING + UIIMAGE;
  • NSDATA = NSATTRIBUTED STRING;
  • ALSO I can convert nsdata to nsattributed string
  • NSATTRIBUTED STRING = NSDATA:
  • And then extract nesting from NSAttributed string
  • NSSTRING = [string NSATTRIBUTED STRING];

Query:

How can I get IMAGES from NSATTRIBUTED STRING,

  • UIIMAGE = of NSATTRIBUTED STRING;
  • ARRAYOFIMAGE = of NSATTRIBUTED STRING;
+4
ios uiimage nsattributedstring
source share
3 answers

You need to list NSAttributedString to search for NSTextAttachment s.

 NSMutableArray *imagesArray = [[NSMutableArray alloc] init]; [attributedString enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, [attributedString length]) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) { if ([value isKindOfClass:[NSTextAttachment class]]) { NSTextAttachment *attachment = (NSTextAttachment *)value; UIImage *image = nil; if ([attachment image]) image = [attachment image]; else image = [attachment imageForBounds:[attachment bounds] textContainer:nil characterIndex:range.location]; if (image) [imagesArray addObject:image]; } }]; 

As you can see, there is an if ([attachment image]) test. This is because if you created an NSTextAttachment for installation with NSAttachmentAttributeName , it will exist and your image will be there. But if you use, for example, an image from the Internet and convert it as an NSTextAttachment from HTML code, then the [attachment image] will be zero and you will not be able to get the image.

You can see it using breakpoints with this snippet (with setting the real image URL and the real image name from the package. NSString * htmlString = @ "http: // anImageURL \"> Blahttp: // anOtherImageURL \ "> Test retest ";

 NSError *error; NSAttributedString *attributedStringFromHTML = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)} documentAttributes:nil error:&error]; NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; [textAttachment setImage:[UIImage imageNamed:@"anImageNameFromYourBundle"]]; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:attributedStringFromHTML]; [attributedString appendAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment]]; 
+14
source share

In Swift 3: (with macOS equivalent here )

 func textViewDidChange(_ textView: UITextView) { // other code... let range = NSRange(location: 0, length: textView.attributedText.length) if (textView.textStorage.containsAttachments(in: range)) { let attrString = textView.attributedText var location = 0 while location < range.length { var r = NSRange() let attrDictionary = attrString?.attributes(at: location, effectiveRange: &r) if attrDictionary != nil { // Swift.print(attrDictionary!) let attachment = attrDictionary![NSAttachmentAttributeName] as? NSTextAttachment if attachment != nil { if attachment!.image != nil { // your code to use attachment!.image as appropriate } } location += r.length } } } } 
+4
source share

I converted the Larme code to fast

  var imagesArray = [Any]() textView.attributedText.enumerateAttribute(NSAttachmentAttributeName, in: NSRange(location: 0, length: textView.attributedText.length), options: [], using: {(value,range,stop) -> Void in if (value is NSTextAttachment) { let attachment: NSTextAttachment? = (value as? NSTextAttachment) var image: UIImage? = nil if ((attachment?.image) != nil) { image = attachment?.image } else { image = attachment?.image(forBounds: (attachment?.bounds)!, textContainer: nil, characterIndex: range.location) } if image != nil { imagesArray.append(image) } } }) 
+3
source share

All Articles