加载gif图过渡效果

摘要:
加载gif图像,转换效果:调用:-(id)initWithGifView:(UIView*)view{self=[supeitWithView:view];if(self){self.color=[UIColorclearColor];NSString*filePath=[[NSBundlebandleWithPath:[[NSBuntlemainBundle]bundlePath]]路径

加载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图过渡效果》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇网站开发(周四):项目后台数据管理(实战)Web Deploy (msdeploy) 使用方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

泛微E8升级E9代码修改

E8升级E9需要修改后台二次开发的代码,总工作量相当大。CheckCustomize=function(){varnodeId=$.val();//应用程序节点如果{varpgrcontrol=$.val);varfinishpercent=$.vl();如果{如果{Dialog.alert(“进度控制为yes,需要完成百分比!”);return false...

试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)

解决方法:iis应用程序池--˃高级设置--˃启用32位应用程序˂!body{font-family:"Verdana";font-weight:normal;font-size:.7em;color:black;}p{font-family:"Verdana";font-weight:normal;color:black;margin-top:-5px}b...

echarts折线图 鼠标移入改变小点显示样式

=undefined){res+=nameList[i].seriesName+':'+nameList[i].data+'%'+''}}res=res.split;returnres[0]+''+res[1];}}echarts折线图的鼠标移动上去小点显示样式修改tooltip:{trigger:'axis',formatter:function{varr...

Sublime Text3注册激活和部分配置

此时,我们可以输入要安装的插件包ConvertToUTF85。设置中文对齐方式、字体等//设置默认代码“default_encoding”:“UTF-8”,//显示代码“show_encoding”:true,//显示行号“show_line_endings”:true,//设置字号“font_size”:14,//设置字体对齐方式“font_options...

jquery跨域请求数据

Jquery跨域请求数据Jquery跨请求数据。事实上,这很容易。请遵循以下步骤:首先,编写js,通过get获取远程数据。请注意,回调参数应添加在链接之后,这意味着将回调函数地址传输到远程页面。',{params},函数cb{alert;alert;},'json');第二:编写处理程序。publicvoidProcessRequest{context.Re...

dBFs和dBm

dBFs和dBmdBFs是用来表征数字域功率值的大小,一般情况下我们定义0dBFs为数字域满刻度功率值,即数字域中功率的最大值;因此看到的dBFs的值都是负的。...