zoukankan      html  css  js  c++  java
  • 用SDWebImage加载FLAnimatedImage

    用SDWebImage加载FLAnimatedImage

    效果

    源码

    https://github.com/YouXianMing/Animations

    //
    //  GifPictureController.m
    //  Animations
    //
    //  Created by YouXianMing on 16/2/21.
    //  Copyright © 2016年 YouXianMing. All rights reserved.
    //
    
    #import "GifPictureController.h"
    #import "FLAnimatedImage.h"
    #import "FLAnimatedImageView.h"
    #import "UIImageView+WebCache.h"
    #import "UIView+SetRect.h"
    #import "GCD.h"
    
    @interface GifPictureController ()
    
    @end
    
    @implementation GifPictureController
    
    - (void)setup {
        
        [super setup];
        
        // https://github.com/Flipboard/FLAnimatedImage
        
        self.contentView.layer.masksToBounds = YES;
        
        FLAnimatedImageView *gifImageView = [[FLAnimatedImageView alloc] init];
        gifImageView.frame                = CGRectMake(0.0, 0.0, 100.0, 100.0);
        [self.contentView addSubview:gifImageView];
        
        __weak GifPictureController *wself = self;
        NSString *imagePath                = @"http://images2015.cnblogs.com/blog/607542/201601/607542-20160123090832343-133952004.gif";
        NSData   *gifImageData             = [self imageDataFromDiskCacheWithKey:imagePath];
        
        if (gifImageData) {
            
            [wself animatedImageView:gifImageView data:gifImageData];
            
        } else {
            
            NSURL *url = [NSURL URLWithString:imagePath];
            [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:url
                                                                  options:0
                                                                 progress:nil
                                                                completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                                                                    
                                                                    [[[SDWebImageManager sharedManager] imageCache] storeImage:image
                                                                                                          recalculateFromImage:NO
                                                                                                                     imageData:data
                                                                                                                        forKey:url.absoluteString
                                                                                                                        toDisk:YES];
                                                                    
                                                                    [[GCDQueue mainQueue] execute:^{
                                                                        
                                                                        [wself animatedImageView:gifImageView data:data];
                                                                    }];
                                                                }];
        }
    }
    
    - (void)animatedImageView:(FLAnimatedImageView *)imageView data:(NSData *)data {
    
        FLAnimatedImage *gifImage = [FLAnimatedImage animatedImageWithGIFData:data];
        imageView.frame           = CGRectMake(0, 0, gifImage.size.width, gifImage.size.height);
        imageView.center          = self.contentView.middlePoint;
        imageView.animatedImage   = gifImage;
        imageView.alpha           = 0.f;
        
        [UIView animateWithDuration:1.f animations:^{
            
            imageView.alpha = 1.f;
        }];
    }
    
    - (NSData *)imageDataFromDiskCacheWithKey:(NSString *)key {
        
        NSString *path = [[[SDWebImageManager sharedManager] imageCache] defaultCachePathForKey:key];
        return [NSData dataWithContentsOfFile:path];
    }
    
    @end

    细节

  • 相关阅读:
    使用序列化实现对象的拷贝
    理解 Java 的三大特性之多态
    LeetCode OJ:Add and Search Word
    关于Qt的事件循环以及QEventLoop的简单使用
    LeetCode OJ:Generate Parentheses(括号生成)
    LeetCode OJ:Maximal Square(最大矩形)
    LeetCode OJ:Range Sum Query 2D
    LeetCode OJ:Power of Two(2的幂)
    LeetCode OJ:Longest Increasing Subsequence(最长递增序列)
    LeetCode OJ:Ugly Number II(丑数II)
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/5208142.html
Copyright © 2011-2022 走看看