0
votes

I used SD_WebImage to show My gif, like this:

code:

    UIImage *gifImg = [UIImage sd_animatedGIFWithData:[NSData dataWithContentsOfFile:imagePath]];


    NSTextAttachment *atc = [[NSTextAttachment alloc] initWithData:nil ofType:nil];

    atc.image = image;

    NSAttributedString *textAttachmentString = [NSAttributedString attributedStringWithAttachment:atc];

 [emojiText replaceCharactersInRange:range withAttributedString:textAttachmentString];

MyTextView.attributedString = emojiText;

Result: the image display on my UITextView successfully, but it dose not play, it just be static

why? how can i make this gif-image active?

(btw: with some reasons i cannot use YYkit in my project, so I must use the UITextView or UILabel) I know that imageView can play animation with an array of images. But i does not find the correct way to show an imageView in textView.

Help me~????

1
You can check the answer here. stackoverflow.com/questions/29949348/…Quoc Nguyen

1 Answers

0
votes

finally i solved thir problem

i use an empty attachment replace my gif-image_script

then i find this attachment, create a yyanimatedimage and put it on my textview

[mAttributedString enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, mAttributedString.length) options:0 usingBlock:^(id _Nullable value, NSRange range, BOOL * _Nonnull stop) {
    if ([value isKindOfClass:[NSTextAttachment class]]) {
        NSTextAttachment * attachment = value;
        if ([attachment.tagExtra isEqualToString:@"gif"]) {
            label.selectedRange = range;
            CGRect rect = [label firstRectForRange:label.selectedTextRange];
            rect.origin.y += (rect.size.height - 20);
            rect.size = CGSizeMake(20, 20);

            YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithFrame:rect];
            [label addSubview:imageView];
            imageView.image = [YYImage imageNamed:attachment.tagName];
        }
    }
}];
label.selectedRange = NSMakeRange(0, 0);