10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SDWebImageを使って表示画像をキャッシュし、ネットワークから取得する場合はProgressを表示する

Last updated at Posted at 2015-09-03

インスタっぽい画像表示をつくりたいと思って実装。
CircleProgressView(https://github.com/CardinalNow/iOS-CircleProgressView)とお馴染みSDWebImage(https://github.com/rs/SDWebImage)を使用。

        //メンバ変数をnilにしておく
        self.entriesImage.image = nil;
        SDWebImageManager *imageManager = [SDWebImageManager sharedManager];
        [imageManager.imageCache queryDiskCacheForKey:self.entryModel.image_url done:^(UIImage *image, SDImageCacheType cacheType)
        {
            if (image) {
                NSLog(@"キャッシュがある");
                self.circleProgressView.hidden = YES;
                //取得したimageをメンバ変数へ代入
                self.entriesImage.image = image; 
            } else {
                //キャッシュがないのでネットワークから読み出すなどの処理をする
                [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:self.entryModel.image_url]
                                                                      options:0
                                                                     progress:
                 ^(NSInteger receivedSize, NSInteger expectedSize) {
                     self.circleProgressView.hidden = NO;
                     float progress = [@(receivedSize) floatValue]/[@(expectedSize) floatValue];

                     self.circleProgressView.progress = progress;
                 } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                     if( error ){
                         return;
                     }
                     
                     if (finished){
                         SDImageCache *imageCache = [[SDWebImageManager sharedManager]imageCache];
                         [imageCache storeImage:image forKey:self.entryModel.image_url];
                         
                         self.circleProgressView.hidden = YES;
                         self.entriesImage.image = image;
                     }
                     
                 }];
            }
        }];

◼︎参考
http://hack.aipo.com/archives/9983/
http://blogios.stack3.net/archives/288
https://xn--t8j4aa4npge9cynpab8kvj.jp/
http://blog.waft.me/ios-http-cache/
https://www.xn--t8j4c7dcf6169bud5f.jp/

10
10
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
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?