【问题标题】:Extract UIImage from NSAttributed String从 NSAttributedString 中提取图像
【发布时间】:2015-03-19 18:47:46
【问题描述】:

我正在做的是:

  1. NSATTRIBUTE STRING = NSSTRING + UIIMAGE 的;
  2. NSDATA = NSATTRIBUTED 字符串;
  3. 我还可以将 nsdata 转换为 nsattributed 字符串
  4. NSATTRIBUTED 字符串 = NSDATA:
  5. 然后从 NSAttributed 字符串中提取嵌套
  6. NSSTRING = [NSATTRIBUTED STRING 字符串];

查询:

如何从 NSATTRIBUTED STRING 中获取图像;

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

【问题讨论】:

    标签: ios uiimage nsattributedstring


    【解决方案1】:

    您必须枚举NSAttributedString 寻找NSTextAttachments。

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

    如您所见,有测试if ([attachment image])。那是因为似乎如果您创建了 NSTextAttachment 并与 NSAttachmentAttributeName 放在一起,它将存在并且您的图像将在那里。但是,如果您使用例如来自网络的图像并将其从 HTML 代码转换为 NSTextAttachment,则 [attachment image] 将为 nil,您将无法获取该图像。

    您可以通过这个 sn-p 看到使用断点(通过设置真实图像 URL 和来自 bundle.xml 的真实图像名称)。 NSString *htmlString = @"http://anImageURL\">Blahttp://anOtherImageURL\"> 测试 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]];
    

    【讨论】:

    • 谢谢。我将实施给定的解决方案并更新您。
    • 非常感谢。即使我设置了自己的文本附件,当我再次尝试提取它们时,image 为零,但imageForBounds 有效!那里没有太多信息,这对我来说似乎是一个错误。
    • @larme 如何在标签中设置
    • @MssiOS 你能更明确一点吗?您的解决方案到底有什么问题?你的问题和作者的问题类似吗?
    【解决方案2】:

    在 Swift 3 中:(与 macOS 等效的 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
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      我将代码 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)
                      }
                  }
              })
      

      【讨论】:

        猜你喜欢
        • 2018-10-30
        • 1970-01-01
        • 2017-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-29
        • 1970-01-01
        相关资源
        最近更新 更多