Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
10
Help us understand the problem. What is going on with this article?
@yu01

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

More than 1 year has passed since last update.

インスタっぽい画像表示をつくりたいと思って実装。
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
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
yu01
radineer
メディア運営をしています。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
10
Help us understand the problem. What is going on with this article?