多平台保持统一风格的UI设计,少不了一些动态图片的使用

1、本地GIF图片使用

1.1 将本地GIF转为NSdata类型

  NSData *tempdata = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"networkLoading" ofType:@"gif"]];
UIImageView *imgView = [[UIImageView alloc] initWithImage:[self decodeGifImageByData:tempdata]];

1.2 通过自定义方法处理GIF的data数据

 //解压gif的data为图片数组
-(UIImage *)decodeGifImageByData:(NSData *)data {
//获取data资源器,这个可以直接操作图片data
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, nil);
//获取图片数量 size_t 类似于无符号int
size_t count = CGImageSourceGetCount(source);
UIImage * animationImage; //如果只有一张则直接解压
if (count <= ) {
animationImage = [[UIImage alloc]initWithData:data];
}
else {
NSMutableArray * imageArray = [NSMutableArray arrayWithCapacity:count];
NSTimeInterval duration = 0.0f;
//遍历获取每一张图片
for (size_t i = ; i < count; i++) {
//解析图片拿到图片画笔拿到图片画笔
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL);
duration += [self imageDurationAtIndex:i source:source];
//scale:图片缩放因子 默认1 orientation:图片绘制方向 默认网上
[imageArray addObject:[UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
CGImageRelease(imageRef);
} //如果没有抓取到图片播放时间,则按照0.1秒一张的方式来了播放
if (!duration) {
duration = (1.0f / 10.0f) * count;
}
animationImage = [UIImage animatedImageWithImages:imageArray duration:duration];
} CFRelease(source);
return animationImage;
} //获取每一张图片的持续时间
-(float)imageDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source {
float imageDuration = 0.1f;
//获取指定图像的属性信息 如宽、高、持续时间等都在里面 详情参考 CGImageProperties
CFDictionaryRef cfImageProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);
if (!cfImageProperties) {
return imageDuration;
}
NSDictionary * imageProperties = (__bridge NSDictionary *) cfImageProperties;
//拿到gif图的属性信息 获取每一帧的时间
NSDictionary * gifProperties = [imageProperties valueForKey:(NSString *)kCGImagePropertyGIFDictionary];
NSNumber * delayTime = [gifProperties valueForKey:(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
if (delayTime != nil) {
return delayTime.floatValue;
} return imageDuration;
}
05-22 11:41