ios 从NSAttributed字符串中提取UIImage

ecbunoof  于 2023-04-08  发布在  iOS
关注(0)|答案(3)|浏览(142)

我正在做的是:

  1. NSATTRIBUTE STRING = NSSTRING + UIIMAGE's;
  2. String = String;
    1.我还可以将nsdata转换为nsattributed字符串
  3. NSATTRIBUTED字符串= NSDATA:
    1.然后从NSAttributed字符串中提取嵌套
  4. NSSTRING = [NSATTRIBUTED STRING string];
    查询:
    如何从NSATTRIBUTED字符串中获取图像;
  • UIIMAGE = from NSATTRIBUTED String;
  • ARRAYOFIMAGE = from NsattribTED STRING;
mzmfm0qo

mzmfm0qo1#

您必须枚举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放在一起,它就会存在,你的图像也会在那里。但是如果你使用例如来自Web的图像并将其从HTML代码转换为NSTextAttachment,那么[attachment image]将为nil,你将无法获得图像。
您可以看到在此代码段中使用断点(通过设置真实的图像URL和来自bundle的真实图像名称。NSString *htmlString = @“http://anImageURL"〉Blahtp://anOtherImageURL"〉Testretest";

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

jutyujz02#

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

mwkjh3gx3#

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

相关问题