1.用了SDWebImage的加载本地
NSString*filepath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"测试"ofType:@"gif"];
NSData*imagedata = [NSData dataWithContentsOfFile:filepath];
UIImageView* loadingImage = [[UIImageView alloc] initWithFrame:CGRectMake(10,10, 300, 280)];
loadingImage.image= [UIImage sd_animatedGIFWithData:imagedata];
[self.view addSubview: loadingImage];
2.加载网络GIF图片 (SDWebImage 4.0 之前)与(SDWebImage 4.0 之后)分别为:
2.1 SDWebImage 4.0 之前:
UIImageView*loadingImgeView = [[UIImageView alloc] initWithFrame:CGRectMake((80,10,350, 300)];
[loadingImgeView sd_setImageWithURL:[NSURL URLWithString:@"https://cloud.githubusercontent.com/assets/1567433/10417835/1c97e436-7052-11e5-8fb5-69373072a5a0.gif"]];
[self.view addSubview:loadingImgeView];
2.1 SDWebImage 4.0 之后:参考GitHubhttps://github.com/Flipboard/FLAnimatedImage
安装:pod 'FLAnimatedImage', '~> 1.0'
2.1.1#import
“self.imageView1”是自定义创建的@property (nonatomic, strong) FLAnimatedImageView *imageView1;
if (!self.imageView1) {
self.imageView1 = [[FLAnimatedImageView alloc] init];
self.imageView1.contentMode = UIViewContentModeScaleAspectFill;
self.imageView1.clipsToBounds=YES;
}
self.imageView1.frame = CGRectMake(0.0, 120.0, self.view.bounds.size.width, 447.0);
[self.view addSubview:self.imageView1];
加载本地:
NSURL *url1 = [[NSBundle mainBundle] URLForResource:@"绿球" withExtension:@"gif"];
NSData *data1 = [NSData dataWithContentsOfURL:url1];
FLAnimatedImage *animatedImage1 = [FLAnimatedImage animatedImageWithGIFData:data1];
self.imageView1.animatedImage= animatedImage1;
加载远程的URL 1.
NSURL *url2 = [NSURL URLWithString:@"https://cloud.githubusercontent.com/assets/1567433/10417835/1c97e436-7052-11e5-8fb5-69373072a5a0.gif"];
[self loadAnimatedImageWithURL:url2 completion:^(FLAnimatedImage *animatedImage) {
self.imageView1.animatedImage = animatedImage;
self.debugView1.image = animatedImage;
self.imageView1.userInteractionEnabled = YES;
}];
// Even though NSURLCache *may* cache the results for remote images, it doesn't guarantee it.
/// Cache control headers or internal parts of NSURLCache's implementation may cause these images to become uncache.
/// Here we enfore strict disk caching so we're sure the images stay around.
- (void)loadAnimatedImageWithURL:(NSURL*const)url completion:(void(^)(FLAnimatedImage*animatedImage))completion
{
NSString *const filename = url.lastPathComponent;
NSString *const diskPath = [NSHomeDirectory() stringByAppendingPathComponent:filename];
NSData * __block animatedImageData = [[NSFileManager defaultManager] contentsAtPath:diskPath];
FLAnimatedImage * __block animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:animatedImageData];
if(animatedImage) {
if(completion) {
completion(animatedImage);
}
}else{
[[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
animatedImageData = data;
animatedImage = [[FLAnimatedImagealloc]initWithAnimatedGIFData:animatedImageData];
if(animatedImage) {
if(completion) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(animatedImage);
});
}
[datawriteToFile:diskPathatomically:YES];
}
}]resume];
}
}
最后提示大家避坑总结:
1.新版本SDWebImage不再支持原先的方式了,用他们之前弄了一个单独的类FLAnimatedImage来播放gif。
2.参考GitHubhttps://github.com/Flipboard/FLAnimatedImage
3.NSBundle获取不了本地的Assets照片路径,是因为Deployment Target 9.0之后就有这个问题,所以你要把加载的gif要直接放在项目中