开发文档

FLAnimatedImageView加载卡顿问题。

2017-12-12  本文已影响4人  大老虎_Robin
-  (void)loadGIFImage {
    FLAnimatedImageView *gifImgView = [[FLAnimatedImageView alloc] init];
    NSString *imagePath = @"gif图片地址";
    NSData *gifImageData = [self imageDataFromDiskCacheWithKey:imagePath];
    
    if (gifImageData) {
        [self animatedImageView:gifImgView
                           data:gifImageData];
    } else {
        @weakify(self);
        NSURL *url = [NSURL URLWithString:imagePath];
        [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:url
                                                              options:0
                                                             progress:nil
                                                            completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                                                                @strongify(self);
                                                                [[[SDWebImageManager sharedManager] imageCache] storeImage:image
                                                                                                                 imageData:nil
                                                                                                                    forKey:url.absoluteString
                                                                                                                    toDisk:NO
                                                                                                                completion:nil];
                                                                
                                                                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                                                                    
                                                                    if (data != nil) {
                                                                        dispatch_async(dispatch_get_main_queue(), ^{
                                                                            [self animatedImageView:gifImgView
                                                                                               data:data];
                                                                        });
                                                                    }
                                                                });
                                                                
                                                            }];
    }
}

- (void)animatedImageView:(FLAnimatedImageView *)imageView data:(NSData *)data {
    
    FLAnimatedImage *gifImage = [FLAnimatedImage animatedImageWithGIFData:data];
    imageView.animatedImage   = gifImage;
}

- (NSData *)imageDataFromDiskCacheWithKey:(NSString *)key {
    
    NSString *path = [[[SDWebImageManager sharedManager] imageCache] defaultCachePathForKey:key];
    return [NSData dataWithContentsOfFile:path];
}

GCD 的调用 转载“iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用”

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
    // 耗时的操作  
    dispatch_async(dispatch_get_main_queue(), ^{  
        // 更新界面  
    });  
});  

Demo:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  
    NSURL * url = [NSURL URLWithString:@"http://avatar.csdn.net/2/C/D/1_totogo2010.jpg"];  
    NSData * data = [[NSData alloc]initWithContentsOfURL:url];  
    UIImage *image = [[UIImage alloc]initWithData:data];  

    if (data != nil) {  

        dispatch_async(dispatch_get_main_queue(), ^{  
            self.imageView.image = image;  
         });  

    }  
});  

上一篇 下一篇

猜你喜欢

热点阅读