1- (void) Viewdidload2 {3 [Super Viewdidload];4 //Do any additional setup after loading the view.5 6 //1: Using third-party libraries7NSData *data = [NSData datawithcontentsoffile:[[nsbundle mainbundle] Pathforresource:@"Moe"OfType:@"gif"]];8Gifview *dataview = [[Gifview alloc] Initwithframe:cgrectmake ( -, -, -, -) Data:data];9 [Self.view Addsubview:dataview];Ten One //or AGifview *pathview =[[gifview alloc] Initwithframe:cgrectmake ( the, -, -, -) Filepath:[[nsbundle Mainbundle] Pathforresource:@"Moe"OfType:@"gif"]]; - [Self.view Addsubview:pathview]; - the //2: Using WebView -NSString *path = [[NSBundle mainbundle] Pathforresource:@"Moe"OfType:@"gif"]; -NSData *gifdata =[NSData Datawithcontentsoffile:path]; -UIWebView *webview = [[UIWebView alloc] Initwithframe:cgrectmake ( the, Max, -, -)]; +Webview.backgroundcolor =[Uicolor Clearcolor]; -Webview.scalespagetofit =YES; +[WebView loaddata:gifdata MIMEType:@"Image/gif"Textencodingname:nil Baseurl:nil]; A [Self.view Addsubview:webview]; at}
Third-party library code
// // // Gifdemo // #import <uikit/uikit.h># Import <imageio/imageio.h> @interface gifview:uiview{cgimagesourceref gif; Nsdictionary *gifproperties; size_t index; size_t count; Nstimer *timer;} -(id ) initWithFrame: (CGRect) frame FilePath: (NSString * -(id ) initWithFrame: (CGRect) frame data: (NSData * @end
////gifview.m//Gifdemo//#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 (__bridge cfurlref) [Nsurl Fileurlwithpath:_filepath], (__bridge 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=Cgimagesourcecreatewithdata ((__bridge cfdataref) _data, (__bridge 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, (__bridge 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);}/*//Only override Drawrect:if your perform custom drawing.//An empty implementation adversely affects performance du Ring animation. -(void) DrawRect: (cgrect) Rect {//Drawing code}*/@end