欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS开发:SDWebImage4.0之后解决加载gif不显示问题

程序员文章站 2022-07-05 23:43:22
之前的使用方法 uiimageview *imgview = [uiimageview new]; imgview.contentmode = uiviewcontentmod...

之前的使用方法

    uiimageview *imgview = [uiimageview new];
    imgview.contentmode = uiviewcontentmodescaleaspectfit;
    imgview.frame = cgrectmake(0, 0, 100, 30);

    nsstring  *filepath = [[nsbundle bundlewithpath:[[nsbundle mainbundle] bundlepath]]pathforresource:@"going" oftype:@"gif"];
    nsdata  *imagedata = [nsdata datawithcontentsoffile:filepath];
    imgview.backgroundcolor = [uicolor clearcolor];
    imgview.image = [uiimage sd_animatedgifwithdata:imagedata];
    [self addsubview:imgview];

sdwebimage4.0之后使用以上的方法显示gif图,会显示不出来。
访问sdwebimage的github网址

animated images (gif) support

starting with the 4.0 version, we rely onflanimatedimageto take care of our animated images. if you use cocoapods, addpod 'sdwebimage/gif'to your podfile. to use it, simply make sure you useflanimatedimageviewinstead ofuiimageview. note: there is a backwards compatible feature, so if you are still trying to load a gif into auiimageview, it will only show the 1st frame as a static image. important: flanimatedimage only works on the ios platform. for os x, usensimageviewwithanimatesset toyesto show the entire animated images andnoto only show the 1st frame. for all the other platforms (tvos, watchos) we will fallback to the backwards compatibility feature described above

使用下面的新方法就可以正常显示gif

    flanimatedimageview *imgview = [flanimatedimageview new];
    imgview.contentmode = uiviewcontentmodescaleaspectfit;
    imgview.frame = cgrectmake(0, 0, 100, 30);
    nsstring  *filepath = [[nsbundle bundlewithpath:[[nsbundle mainbundle] bundlepath]]pathforresource:@"going" oftype:@"gif"];
    nsdata  *imagedata = [nsdata datawithcontentsoffile:filepath];
    imgview.backgroundcolor = [uicolor clearcolor];
    imgview.animatedimage = [flanimatedimage animatedimagewithgifdata:imagedata];
    [self addsubview:imgview];