IOS directly loads GIF and iosgif animations
<Pre name = "code" class = "objc"> // iOS animated GIF animation, with a frame cut and direct display. GIF Image
</Pre> <pre name = "code" class = "objc"> // first, we define a <span style = "font-family: Arial, Helvetica, sans-serif; "> NTVGifAnimationView class to load </span>
# Import <UIKit/UIKit. h> # import <MobileCoreServices/MobileCoreServices. h> # import <ImageIO/ImageIO. h> # define Gif_W 114 # define Lab_H 30 # define Gif_H (114 + Lab_H) @ interface NTVGifAnimationView: UIView {CGImageSourceRef _ gif; // save gif animation NSDictionary * _ gifProperties; // Save the GIF animation property size_t _ index; // the sequence number of the frames starting from the playback of the GIF animation size_t _ count; // The total number of frames of the GIF animation NSTimer * _ timer; // The timer UIView * _ gifView used to play the GIF animation; UILabel * _ label ;}
</Pre> <pre name = "code" class = "objc"> <p class = "p1"> <span class = "s1">/** </span> </p> <p class = "p2"> <span class = "s2"> * initialization method </span> </p> <p class = "p1"> <span class = "s1"> * </span> </p> <p class = "p1"> <span class = "s1"> * @ param frame </span> <span style = "font-family: arial, Helvetica, sans-serif; "> </span> <span style =" font-family: Arial, Helvetica, sans-serif; "> frame </span> </p> <p class =" p1 "> <span class =" s1 "> * @ param <span style =" font-family: arial, Helvetica, sans-serif; "> filePath GIF image address </span> </p> <p class =" p1 "> <span class =" s1 "> * </span> </ p> <p class = "p1"> <span class = "s1"> */</span> </p>-(id) initWithFrame :( CGRect) frame filePath :( NSString *) filePath;-(void) playGif; // play GIF animation-(void) stopGif; // stop GIF animation @ end
</Pre> <br/> <pre name = "code" class = "objc"> # import "NTVGifAnimationView. h "# import <QuartzCore/QuartzCore. h> @ implementation NTVGifAnimationView-(id) initWithFrame :( CGRect) frame filePath :( NSString *) filePath {self = [super initWithFrame: frame]; if (self) {CGRect rect = frame; _ gifView = [[UIView alloc] initWithFrame: CGRectMake (0, 0, rect. size. width, rect. size. height-Lab_H)]; _ gifView. backgroundColor = [UIColor clearColor]; [self addSubview: _ gifView]; _ label = [[UILabel alloc] initWithFrame: CGRectMake (0, CGRectGetMaxY (_ gifView. frame), rect. size. width, Lab_H)]; _ label. textAlignment = NSTextAlignmentCenter; _ label. backgroundColor = [UIColor clearColor]; _ label. text = @ "loading... "; [self addSubview: _ label]; NSDictionary * gifLoopCount = [NSDictionary dictionaryWithObject: [NSNumber numberWithInt: gifLoopCount forKey :( NSString *) kCGImagePropertyGIFDictionary]; _ gif = parse (CFURLRef) [NSURL fileURLWithPath: filePath], (CFDictionaryRef) _ gifProperties ); _ count = CGImageSourceGetCount (_ gif);} return self;}-(void) play {_ index ++; _ index = _ index % _ count; CGImageRef ref = CGImageSourceCreateImageAtIndex (_ gif, _ index, (CFDictionaryRef) _ gifProperties); _ gifView. layer. contents = (_ bridge id) ref; CFRelease (ref);}-(void) dealloc {DLog (@ "dealloc"); CFRelease (_ gif );} -(void) playGif {_ timer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @ selector (play) userInfo: nil repeats: YES]; [_ timer fire];} -(void) stopGif {[_ timer invalidate]; _ timer = nil; [self removeFromSuperview];} @ end
</pre><pre name="code" class="objc">
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.