Gif動態圖UIImage

來源:互聯網
上載者:User

標籤:format   ssi   ati   sdi   fir   more   man   code   web   

#pragma mark 動態圖產生-(UIImage *)getAnimatedGIFWithData:(NSString *)path{    NSData *data = [NSData dataWithContentsOfFile:path];    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 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)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;}

 

Gif動態圖UIImage

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.