5

I had a look at this question and it didn't work. I am also trying to avoid this sort of solution. In the end I am willing to go with a CADisplayLink and change each frame manually. Still, is there a simpler way to do this?

hichris123
  • 10,145
  • 15
  • 56
  • 70
Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • Do you just want an image in the `UITextView` or do you want the GIF to animate? If you're talking about `CADisplayLink` and frames then I assume you want it to animate? – mluisbrown Mar 20 '15 at 13:20
  • I want the Gif to be inside the TextView and (hence being a Gif) to animate. – Rui Peres Mar 20 '15 at 13:22
  • Ok, that's what I thought. Gif's don't have to animate, they can be just a single frame. – mluisbrown Mar 20 '15 at 13:26
  • 3
    @vikingosegundo the problem is not animating a gif, it's actually viewing it animated inside a `UITextView` with `NSTextAttachment`. – Rui Peres Mar 27 '15 at 09:54

2 Answers2

4

An animated gif if just a collection of images which are displayed in sequence at a timed interval. You can do this with a UIImageView like this:

imageView.animationImages = [UIImage(named: "uploadFrame0")!, UIImage(named: "uploadFrame1")!, UIImage(named: "uploadFrame2")!, UIImage(named: "uploadFrame3")!, UIImage(named: "uploadFrame4")!, UIImage(named: "uploadFrame5")!, UIImage(named: "uploadFrame6")!, UIImage(named: "uploadFrame7")!, UIImage(named: "uploadFrame8")!]
imageView.animationDuration = 1
imageView.startAnimating()

Then you can use exclusion paths to make the text flow around it.

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
Bjørn Ruthberg
  • 336
  • 1
  • 15
1

Use exclusion paths to define areas that are kept free, than add the image with an image view.

NSMutableArray *exclutionPaths = [NSMutableArray array];
[exclutionPaths addObject:[UIBezierPath bezierPathWithRect:CGRectInset(imageFrame, -4, 0)]];
[_textView.textContainer setExclusionPaths:exclutionPaths];
[_textView addSubview:imageView];
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178