Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
102 views
in Technique[技术] by (71.8m points)

ios - Extract UIImage from NSAttributed String

What I am doing is:

  1. NSATTRIBUTE STRING = NSSTRING + UIIMAGE's;
  2. NSDATA = NSATTRIBUTED STRING;
  3. ALSO I am able to convert nsdata to nsattributed string
  4. NSATTRIBUTED STRING = NSDATA:
  5. And then extracting nesting from NSAttributed string
  6. NSSTRING = [NSATTRIBUTED STRING string];

Query:

How can i get IMAGES from NSATTRIBUTED STRING;

  • UIIMAGE = from NSATTRIBUTED STRING;
  • ARRAYOFIMAGE = from NSATTRIBUTED STRING;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to enumerate the NSAttributedString looking for 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];
    }
}];

As you can see, there is the test if ([attachment image]). That's because it seems that if you created the NSTextAttachment to put with NSAttachmentAttributeName it will exist and your image will be there. But if you use for example an image from the web and convert it as a NSTextAttachment from a HTML code, then [attachment image] will be nil and you won't be able to get the image.

You can see using breakpoints with this snippet (with setting real image URL and an real image name from bundle. 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]];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...