从NSAttributedstring提取UIImage

我正在做的是:

  1. NSATTRIBUTE STRING = NSSTRING + UIIMAGE的;
  2. NSDATA = NSATTRIBUTED STRING;
  3. 此外,我能够将nsdata转换为nsattributedstring
  4. NSATTRIBUTED STRING = NSDATA:
  5. 然后从NSAttributedstring中提取嵌套
  6. NSSTRING = [NSATTRIBUTED STRING string];

查询:

我怎样才能从NSATTRIBUTED STRING获得图像;

  • UIIMAGE =来自NSATTRIBUTED STRING;
  • ARRAYOFIMAGE =来自NSATTRIBUTED STRING;

你必须枚举NSAttributedString寻找NSTextAttachment

 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]; } }]; 

正如你所看到的,有testingif ([attachment image]) 。 那是因为看起来如果你创build了NSTextAttachment来放置NSAttachmentAttributeName它将会存在,你的图像将会在那里。 但是,如果您使用networking中的图像,并将其转换为HTML代码中的NSTextAttachment ,则[attachment image]将为零,您将无法获取图像。

你可以看到使用这个断点的断点(设置真正的图像URL和一个真正的图像名称从捆绑软件NSString * htmlString = @“http:// anImageURL \”> Blahttp:// anOtherImageURL \“>testing重新testing ”;

 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]]; 

在Swift 3中(与这里的 macOS相当)

 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 } } } } 

我把代码Larme转换成swift 3

  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) } } })