加载gif图片,过渡效果:

调用:

- (id)initWithGifView:(UIView *)view

{

self = [super initWithView:view];

if (self) {

self.color = [UIColor clearColor];

NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"loading2.gif" ofType:nil];

NSData  *imageData = [NSData dataWithContentsOfFile:filePath];

UIImageView *loadingImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 110, 90)];

loadingImgView.contentMode = UIViewContentModeCenter;

loadingImgView.image = [UIImage sd_animatedGIFWithData:imageData];

self.customView = loadingImgView;

self.mode = MBProgressHUDModeCustomView;

[view addSubview:self];

[self show:YES];

}

return self;

}

封装:

+ (UIImage *)sd_animatedGIFWithData:(NSData *)data {

if (!data) {

return nil;

}

CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);

size_t count = CGImageSourceGetCount(source);

UIImage *animatedImage;

if (count <= 1) {

animatedImage = [[UIImage alloc] initWithData:data];

}

else {

NSMutableArray *images = [NSMutableArray array];

NSTimeInterval duration = 0.0f;

for (size_t i = 0; i < count; i++) {

CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);

duration += [self sd_frameDurationAtIndex:i source:source];

[images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];

CGImageRelease(image);

}

if (!duration) {

duration = (1.0f / 10.0f) * count;

}

animatedImage = [UIImage animatedImageWithImages:images duration:duration];

}

CFRelease(source);

return animatedImage;

}

+ (float)sd_frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source {

float frameDuration = 0.1f;

CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);

NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties;

NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyGIFDictionary];

NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime];

if (delayTimeUnclampedProp) {

frameDuration = [delayTimeUnclampedProp floatValue];

}

else {

NSNumber *delayTimeProp = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime];

if (delayTimeProp) {

frameDuration = [delayTimeProp floatValue];

}

}

// Many annoying ads specify a 0 duration to make an image flash as quickly as possible.

// We follow Firefox's behavior and use a duration of 100 ms for any frames that specify

// a duration of <= 10 ms. See <rdar://problem/7689300> and <http://webkit.org/b/36082>

// for more information.

if (frameDuration < 0.011f) {

frameDuration = 0.100f;

}

CFRelease(cfFrameProperties);

return frameDuration;

}

+ (UIImage *)sd_animatedGIFNamed:(NSString *)name {

CGFloat scale = [UIScreen mainScreen].scale;

if (scale > 1.0f) {

NSString *retinaPath = [[NSBundle mainBundle] pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"gif"];

NSData *data = [NSData dataWithContentsOfFile:retinaPath];

if (data) {

return [UIImage sd_animatedGIFWithData:data];

}

NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];

data = [NSData dataWithContentsOfFile:path];

if (data) {

return [UIImage sd_animatedGIFWithData:data];

}

return [UIImage imageNamed:name];

}

else {

NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];

NSData *data = [NSData dataWithContentsOfFile:path];

if (data) {

return [UIImage sd_animatedGIFWithData:data];

}

return [UIImage imageNamed:name];

}

}

- (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size {

if (CGSizeEqualToSize(self.size, size) || CGSizeEqualToSize(size, CGSizeZero)) {

return self;

}

CGSize scaledSize = size;

CGPoint thumbnailPoint = CGPointZero;

CGFloat widthFactor = size.width / self.size.width;

CGFloat heightFactor = size.height / self.size.height;

CGFloat scaleFactor = (widthFactor > heightFactor) ? widthFactor : heightFactor;

scaledSize.width = self.size.width * scaleFactor;

scaledSize.height = self.size.height * scaleFactor;

if (widthFactor > heightFactor) {

thumbnailPoint.y = (size.height - scaledSize.height) * 0.5;

}

else if (widthFactor < heightFactor) {

thumbnailPoint.x = (size.width - scaledSize.width) * 0.5;

}

NSMutableArray *scaledImages = [NSMutableArray array];

UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);

for (UIImage *image in self.images) {

[image drawInRect:CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledSize.width, scaledSize.height)];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

[scaledImages addObject:newImage];

}

UIGraphicsEndImageContext();

return [UIImage animatedImageWithImages:scaledImages duration:self.duration];

}

我调用的类是继承MBProgressHUD的。大家看的时候自己在研究下。

加载gif图过渡效果的更多相关文章

  1. Android 高清加载巨图方案 拒绝压缩图片

    Android 高清加载巨图方案 拒绝压缩图片 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/49300989: 本文出自:[张 ...

  2. Unity使用脚本进行批量动态加载贴图

    先描述一下我正在做的这个项目,是跑酷类音游. 那么跑酷类音游在绘制跑道上的时候,就要考虑不同的砖块显示问题.假设我有了一个节奏列表,那么我们怎么将不同的贴图贴到不同的砖块上去呢? 我花了好几个小时才搞 ...

  3. 简易仿ios菊花加载loading图

    原文链接:https://mp.weixin.qq.com/s/wBbQgOfr59wntNK9ZJ5iRw 项目中经常会用到加载数据的loading显示图,除了设计根据app自身设计的动画loadi ...

  4. Android内存优化————加载长图

    项目中总会遇到加载长图的需求,图片的长度可能是手机长度的很多倍,也就是需要通过滑动来查看图片.比较简单的实现方式就是使用ScrollView来加载长图,但是这样做有一个很严重的问题,就是内存消耗严重. ...

  5. SurfaceView加载长图

    1:SurfaceView加载长图,移到.可以充当背景 效果截图 2:View (淡入淡出动画没实现:记录下) package com.guoxw.surfaceviewimage; import a ...

  6. React-Native 之 GD (二十)removeClippedSubviews / modal放置的顺序 / Android 加载git图\动图 / 去除 Android 中输入框的下划线 / navigationBar

    1.removeClippedSubviews 用于提升大列表的滚动性能.需要给行容器添加样式overflow:’hidden’.(Android已默认添加此样式)此属性默认开启 这个属性是因为在早期 ...

  7. H5异步加载多图

    异步加载多图(可能没啥用,加载慢)(图片预加载,提前给浏览器缓存图片) 1. 用一个计数变量记录需要加载的图片个数 2. 用new Image()去加载,加载完给此对象的src赋值要加载的url路径( ...

  8. Android 高清加载巨图方案, 拒绝压缩图片

    源地址:http://blog.csdn.net/lmj623565791/article/details/49300989 一.概述 距离上一篇博客有段时间没更新了,主要是最近有些私事导致的,那么就 ...

  9. 关于JVM加载内存图学习小密招

    先看如下代码: Person.java public class Person { private String name; private int age; static int count = 0 ...

随机推荐

  1. EasyUi DataGrid中数据编辑方式及编辑后数据获取,校验处理

    EasyUi中的DataGrid提供前台编辑的相关函数. 实现客户选中DataGrid中一列后,对选中列中的一个字段进行编辑,并对数据进行实时校验后,传递至后台保存的需求, 主要涉及到DataGrid ...

  2. Go语言之defer

    defer语句被用于预定对一个函数的调用.我们把这类被defer语句调用的函数称为延迟函数.注意,defer语句只能出现在函数或方法的内部. 一条defer语句总是以关键字defer开始.在defer ...

  3. window.open窗口居中和窗口最大化

    1.window.open()参数 window.open(pageURL,name,parameters) 其中: pageURL为子窗口路径 name为子窗口句柄 parameters为窗口参数( ...

  4. 乱七八糟 Nodejs 系列一:试水

    看Nodejs比较舒服的人,个人认为主要是: 以前是后端,转成前端的人: 前端巨牛的人: 后端巨牛的人: 巨牛的人... 当然还有我这种,脑抽不止的人~~ 不过学习的过程中发现,如果不是上来就用exp ...

  5. 使用PHP对数据库输入进行恶意代码清除

    这是一个有用的PHP函数清理了所有的输入数据,并删除代码注入的几率. function sanitize_input_data($input_data) { $input_data = trim(ht ...

  6. 文成小盆友python-num6 -反射 ,常用模块

    本次主要内容: 内容补充 python中的反射 常用模块 一,内容补充: 利用上次说到的递归的方法来实现阶乘. 说明:利用函数递归的方法来实现阶乘如: 1*2*3*4*5*6*7 代码实现如下: de ...

  7. Repeater绑定数据库,使用AspNetPager进行分页

    分页是Web中经常遇到的功能,分页主要有真分页和假分页. 所谓真分页是指:每一页显示多少数据,就从数据库读多少数据: 假分页是指:一次性从数据库读取所有数据,然后再进行分页. 这两种分页方式区别在于从 ...

  8. C语言初学 计算二元一次方程的问题

    #include<stdio.h> #include<math.h> int main() { double a,b,c,disc,x1,x2; scanf("%lf ...

  9. m2eclipse插件安装

    一.给Eclipse安装maven的插件 m2eclipse 1 打开eclipse 2 Help -->Eclipse MarketPlace,在打开的界面搜索框中输入maven查找m2ecl ...

  10. hdu Examining the Rooms

    这道题的知识点第一次听说 ,就是应用斯特林数.题目的意思是给你房间数N,和最多能破门的个数,让你求能全部把房间打开的概率! a[i][j]=a[i-1][j-1]+(i-1)*a[i-1][j]; # ...