Three ways to load GIF dynamic graphs

Source: Internet
Author: User

Preparation: Local photo resources, Gifview

Gifview Code:

/** * Start playing the animation at the end of the call, and if you need to specify when to play it, just put the timer's start in the right place.     By changing the Cfdictonaryraf, or gifproperties, we can also control whether the animation loops and how many times the loop stops. By changing the index, you can also control how the animation starts playing from a frame.    Similarly, you can also control the playback from a frame to a frame by changing both index and count. Note:-(void) stopgif; Before you can exit this class. Otherwise, the timer does not shut down, resulting in a memory leak. */#import<UIKit/UIKit.h>#import<ImageIO/ImageIO.h>@interfacegifview:uiview {cgimagesourceref gif;//Save GIF animationsNsdictionary *gifproperties;//Save GIF Animation Propertiessize_t index;//the frame ordinal of the start of the GIF animation playbacksize_t count;//the total number of frames for an animated GIFNstimer *timer;//the timer used to play the GIF animation}- (ID) initWithFrame: (CGRect) frame FilePath: (NSString *) _filepath;- (ID) initWithFrame: (CGRect) frame data: (NSData *) _data;- (void) Stopgif;
#import "GifView.h"#import<QuartzCore/QuartzCore.h>@implementationGifview- (ID) initWithFrame: (CGRect) frame FilePath: (NSString *) _filepath{ Self=[Super Initwithframe:frame]; if(self) {gifproperties= [Nsdictionary dictionarywithobject:[nsdictionary dictionarywithobject:[nsnumber numberWithInt:0] Forkey: (NSString *) Kcgimagepropertygifloopcount] Forkey: (NSString*) kcgimagepropertygifdictionary]; GIF=Cgimagesourcecreatewithurl (cfurlref) [Nsurl Fileurlwithpath:_filepath], (cfdictionaryref) gifProperties); Count=cgimagesourcegetcount (GIF); Timer= [Nstimer scheduledtimerwithtimeinterval:0.12target:self selector: @selector (play) Userinfo:nil Repeats:yes];    [Timer fire]; }    returnSelf ;}- (ID) initWithFrame: (CGRect) frame data: (NSData *) _data{ Self=[Super Initwithframe:frame]; if(self) {gifproperties= [Nsdictionary dictionarywithobject:[nsdictionary dictionarywithobject:[nsnumber numberWithInt:0] Forkey: (NSString *) Kcgimagepropertygifloopcount] Forkey: (NSString*) kcgimagepropertygifdictionary]; //gif = Cgimagesourcecreatewithurl ((cfurlref) [Nsurl Fileurlwithpath:_filepath], (cfdictionaryref) gifProperties); GIF =Cgimagesourcecreatewithdata ((cfdataref) _data, (cfdictionaryref) gifproperties); Count=cgimagesourcegetcount (GIF); Timer= [Nstimer scheduledtimerwithtimeinterval:0.12target:self selector: @selector (play) Userinfo:nil Repeats:yes];    [Timer fire]; }    returnSelf ;}-(void) play{Index++; Index= index%count; Cgimagerefref=cgimagesourcecreateimageatindex (GIF, index, (cfdictionaryref) gifproperties); Self.layer.contents= (__bridgeID)ref; Cfrelease (ref);}-(void) removefromsuperview{NSLog (@"Removefromsuperview");    [Timer invalidate]; Timer=Nil; [Super Removefromsuperview];}- (void) dealloc {NSLog (@"Dealloc"); Cfrelease (GIF);}- (void) stopgif{[timer invalidate]; Timer=Nil;}

Three ways to load GIF: (from network or local)

-(NSData *) Loaddataforindex: (Nsinteger) index {nsdata*data =Nil; if(Index = =0) {        //Networkdata = [NSData datawithcontentsofurl:[nsurl urlwithstring:@"http://s14.sinaimg.cn/mw690/005APVsyzy6MFOsVFfv5d&690"]]; }Else {        //Localdata = [NSData datawithcontentsoffile:[[nsbundle mainbundle] Pathforresource:@"Run"OfType:@"gif"]]; }    returndata;}

1.GifView

    // third-party gifview (realizing GIF animation playback is by reading the animation file to Cgimagesourceref and then playing it with Nstimer. )        //-(ID) initWithFrame: (CGRect) frame FilePath: (NSString *) _filepath;    Gifview *dataview = [[Gifview alloc] Initwithframe:cgrectmake (00 ) Data:data];    [Self.view Addsubview:dataview]; //     [DataView stopgif];

2.webView (memory problem not present)

    // WebView    UIWebView *webview = [[UIWebView alloc] Initwithframe:cgrectmake (0  )];     = [Uicolor redcolor];     = YES;    [WebView loaddata:data MIMEType: @" Image/gif " Textencodingname:nil Baseurl:nil];    [Self.view Addsubview:webview];

3. Frame animation

- (void) rungifforimage {Uiimageview*gifimageview = [[Uiimageview alloc] Initwithframe:cgrectmake (0, -, -, -)]; Nsarray*gifarray = [Nsarray arraywithobjects:[uiimage imagenamed:@"1"], [UIImage imagenamed:@"2"], [UIImage imagenamed:@"3"], [UIImage imagenamed:@"4"], [UIImage imagenamed:@"5"], [UIImage imagenamed:@"6"], [UIImage imagenamed:@"7"], [UIImage imagenamed:@"8"], [UIImage imagenamed:@"9"], [UIImage imagenamed:@"Ten"], [UIImage imagenamed:@" One"], [UIImage imagenamed:@" A"], [UIImage imagenamed:@" -"], [UIImage imagenamed:@" -"], [UIImage imagenamed:@" the"], [UIImage imagenamed:@" -"], [UIImage imagenamed:@" -"], [UIImage imagenamed:@" -"], [UIImage imagenamed:@" +"], [UIImage imagenamed:@" -"], [UIImage imagenamed:@" +"], [UIImage imagenamed:@" A"],nil]; Gifimageview.animationimages= Gifarray;//animated Image ArrayGifimageview.animationduration =5;//the length of time required to perform a full animationGifimageview.animationrepeatcount =999;//number of animation repetitions[Gifimageview startanimating]; [Self.view Addsubview:gifimageview];}

Three ways to load GIF dynamic graphs

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.