从NSAttributedString中提取图像

时间:2015-03-19 18:47:46

标签: ios uiimage nsattributedstring

我在做的是:

  1. NSATTRIBUTE STRING = NSSTRING + UIIMAGE;
  2. NSDATA = NSATTRIBUTED STRING;
  3. 我也能将nsdata转换为nsattributed string
  4. NSATTRIBUTED STRING = NSDATA:
  5. 然后从NSAttributed字符串
  6. 中提取嵌套
  7. NSSTRING = [NSATTRIBUTED STRING string];
  8. 查询:

    如何从NSATTRIBUTED STRING获取图像;

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

3 个答案:

答案 0 :(得分:14)

您必须枚举NSAttributedString寻找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];
    }
}];

如您所见,有一个测试if ([attachment image])。那是因为看起来如果您创建了NSTextAttachment以放置NSAttachmentAttributeName它就会存在,您的图像就会存在。但是,如果您使用来自网络的图片并将其从HTML代码转换为NSTextAttachment,则[attachment image]将为零,您将无法获取图片。

您可以看到在此代码段中使用断点(通过设置实际图像URL和捆绑包中的实际图像名称)。     NSString * htmlString = @“http:// anImageURL \”> Blahttp:// anOtherImageURL \“>测试重新测试”;

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

答案 1 :(得分:4)

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

答案 2 :(得分: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)
                }
            }
        })