IOS Display gif

Source: Internet
Author: User
Tags uikit

GIF graph is a kind of dynamic that shows the time of the graph with successive actions in sequence. There is a uniform time to change the next one is not uniform time changes

1. gif images with uniform time changes are better suited to using the IOS system's own method ImageView to display pictures dynamically.

Such as:

Nsmutablearray *array=[nsmutablearray arraywithcapacity:0]; for(intI=1; I < 10;i++) {NSString*str=[nsstring stringWithFormat:@"Nav_bg%d.png", I];
UIImage*image=[UIImage IMAGENAMED:STR];
[Array addobject:image]; } Uiimageview*imageview=[[uiimageview Alloc] Initwithframe:cgrectmake (0,0, the,460)]; Imageview.animationimages=array; imageview.animationduration=1.5;//Duration: Continuous durationImageview.animationrepeatcount=1;//Number of repetitions[Self. window Addsubview:imageview]; [ImageView startanimating];

The code above shows that 10 graphs perform only one animation at 1.5 of the time (not recurring) so the key is that this 1.5 seconds need to express 10 graphs normal execution time can appear the effect is better animation

2. Uncertain time gif

The above method is to execute GIF time according to the given constant time 1.5s, the disadvantage is that if the current method as a generic class method, this 1.5s is very unscientific. So the bottom-up method is to first calculate the total length of time for the picture. Perform the animation again.

#import <UIKit/UIKit.h> @interface motionactiveagegif:uiview{uiimageview *gitview;} @property (nonatomic, Strong) Uiimageview *gitview, @property (nonatomic, Strong) Uiimageview * lastgifview;//@property ( Nonatomic, Strong) NSString * gifname;-(instancetype) Initwithname: (NSString *) gifname andframe: (CGRect) frame; @end # Import "MotionActiveAgeGif.h" #import <ImageIO/ImageIO.h> #import <quartzcore/coreanimation.h>//#define Duringtime 1.5f@interface motionactiveagegif () {floatTotalTime; Cadisplaylink *displaylink;} @end @implementation motionactiveagegif@synthesize gitview; @synthesize lastgifview;-(Instancetype) Initwithname: (    NSString *) Gifname andframe: (cgrect) frame{self = [super init]; if (self) {TotalTime = 0;        Gitview = [[Uiimageview alloc]initwithframe:frame];        Lastgifview = [[Uiimageview alloc]initwithframe:gitview.frame];        Nsurl *url = [[NSBundle mainbundle] urlforresource:[nsstring stringwithformat:@ "%@.gif", GifName] withExtension:nil];        Cgimagesourceref CSF = Cgimagesourcecreatewithurl ((__bridge cftyperef) URL, NULL);        size_t Const COUNT = Cgimagesourcegetcount (CSF);        UIImage *frames[count];                Cgimageref Images[count];        Nsmutablearray *delaytimes = [Nsmutablearray array];            for (size_t i = 0; i < count; ++i) {Images[i] = Cgimagesourcecreateimageatindex (CSF, I, NULL);            UIImage *image =[[uiimage alloc] initwithcgimage:images[i];            Lastgifview.image = image;                        Frames[i] = image; Cfbridgingrelease gives arc ownership nsdictionary *dict = (nsdictionary*) cfbridgingrelease (Cgimagesourcecopypropertiesa            Tindex (CSF, I, NULL)); NSLog (@ "KcgimagepropertyGifdictionary%@ ", [Dict Valueforkey: (nsstring*) kcgimagepropertygifdictionary]);            Nsdictionary *gifdict = [dict valueforkey: (nsstring*) kcgimagepropertygifdictionary];                        [Delaytimes addobject:[gifdict Valueforkey: (nsstring*) kcgimagepropertygifdelaytime]]; if (TotalTime>= 0) {TotalTime=TotalTime+ [[gifdict Valueforkey: (nsstring*) kcgimagepropertygifunclampeddelaytime] floatvalue]; } NSLog (@ "%LF",TotalTime);        Cfrelease (Images[i]); } UIImage *const animation = [UIImage animatedimagewithimages:[nsarray arraywithobjects:frames Count:count] Duratio N:TotalTime];        Gitview.image = animation;  Gitview.animationrepeatcount = 1;        Animation Repeat number 1 does not work [Gitview startanimating];        [Self addsubview:gitview];                Self.frame = Gitview.frame;        [Self performselector: @selector (Stopgifview) Withobject:nil afterdelay:totaltime];    Cfrelease (CSF); } return self;}    -(void) stopgifview{[Gitview Removefromsuperview]; [Self addsubview:lastgifview];}

IOS Display gif

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.