LoginSignup
25
24

More than 5 years have passed since last update.

SDWebImageでGifを再生するとメモリ使用量がヤバかったのでFLAnimatedImageを使った

Posted at

SDWebImageは非常に扱いやすいライブラリで重宝していますが、重いGifを再生するとメモリ使用量がヤバいことになります。
UIWebViewにGifを読ませるという方法もありますが、ロードするときにチラついたりするのでイマイチです。

Gifを再生するときには FLAnimatedImage がオススメです。
FLAnimatedImage

FLAnimatedImageはFlipboardが公開しているライブラリで実際にFlipboardでも使われているようです。
HOW FLIPBOARD PLAYS ANIMATED

ただ、FLAnimatedImage自体はサーバーからのダウンロードには対応していない(?)ようなので
私の場合、GifファイルのダウンロードはSDWebImageを使っています。

pod "FLAnimatedImage"
FLAnimatedImageを使ったGifの再生
#import "FLAnimatedImage.h"
#import "FLAnimatedImageView.h"

FLAnimatedImageView* animatedImageView = [[FLAnimatedImageView alloc] initWithFrame:self.view.bounds];
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:gifURL
                                                      options:0
                                                     progress:^(NSInteger receivedSize, NSInteger expectedSize) {

                                                     } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                                                         if( finished ){
                                                             dispatch_async(dispatch_get_main_queue(), ^{
                                                                 FLAnimatedImage* gifImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:data];
                                                                 animatedImageView.animatedImage = gifImage;
                                                             });
                                                         }
                                                     }];

SDWebImageと比べてメモリ使用量もかなり軽くすることができました。

25
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
24