IOS play animated GIF pictures

Source: Internet
Author: User
Tags uikit

The picture is divided into static and dynamic, there are many kinds of picture formats, In development, it is common for. png and. jpg static images, but sometimes in the app you need to play a dynamic picture, such as a. gif with a small emoji avatar, and no controls to display the animated picture directly in iOS, and here are a few ways to display a dynamic picture.

< a > Uiimageview is used to display pictures, using animated arrays in Uiimageview to animate images

    //Create Uiimageview, add to interfaceUiimageview *imageview = [[Uiimageview alloc] Initwithframe:cgrectmake ( -, -, -, -)];    [Self.view Addsubview:imageview]; //create an array in order to add the picture you want to play ( the picture is a static picture)Nsmutablearray *imgarray =[Nsmutablearray array];  for(intI=1; i<7; i++) {UIImage*image = [UIImage imagenamed:[nsstring stringWithFormat:@"Clock%02d.png", I]];    [Imgarray Addobject:image]; }    //assigning an array of uiimage to an animated imageImageview.animationimages =Imgarray; //set the length of time to perform a full animationImageview.animationduration =6*0.15; //Number of animation repetitions (0 for repeated playback)Imageview.animationrepeatcount =0; //start playing animations[ImageView startanimating]; //Stop play Animation-(void) stopanimating;//Determines whether an animation is being performed-(BOOL) isanimating;

< two > use UIWebView to display animated images

    //get the path to the pictureNSString *path = [[NSBundle mainbundle] Pathforresource:@"Happy"OfType:@"gif"]; //Convert picture to NSDataNSData *gifdata =[NSData Datawithcontentsoffile:path]; //Create a WebView to add to the interfaceUIWebView *webview = [[UIWebView alloc] Initwithframe:cgrectmake (0, Max, $, $)];    [Self.view Addsubview:webview]; //Auto Size adjustmentWebview.scalespagetofit =YES; //No scrollingwebView.scrollView.scrollEnabled =NO; //Set Transparency effectsWebview.backgroundcolor =[Uicolor clearcolor];webview.opaque=0; //Loading Data[WebView loaddata:gifdata MIMEType:@"Image/gif"Textencodingname:nil Baseurl:nil];

  < three > Display local images with third-party Gifview

Gifview is a well-encapsulated class that can be imported directly into a program and then created to display a dynamic picture; it should be noted that Gifview is MRC, so using it in ARC Engineering requires modifying the tag –fno-objc-arc

//Way one: show local gif pictures//Convert a picture to NSData dataNSData *localdata = [NSData datawithcontentsoffile:[[nsbundle mainbundle] Pathforresource:@"Bird"OfType:@"gif"]]; //Create a third-party view display pictureGifview *dataview = [[Gifview alloc] Initwithframe:cgrectmake (0, -, $, -) Data:localdata]; [Self.view Addsubview:dataview];//Way two: show local gif pictures//get the path to the pictureNSString *path = [[NSBundle mainbundle] Pathforresource:@"Cat"OfType:@"gif"];//Create a third-party view display pictureGifview *dataview2 = [[Gifview alloc] Initwithframe:cgrectmake ( $, -, Max, -) Filepath:path]; [Self.view addsubview:dataview2];//mode three: Display GIF pictures obtained from the network//Network PicturesNSData *urldata = [NSData datawithcontentsofurl:[nsurl urlwithstring:@"Http://pic19.nipic.com/20120222/8072717_124734762000_2.gif"]]; //Create a third-party view display pictureGifview *dataviewweb = [[Gifview alloc] Initwithframe:cgrectmake ( -,420,280, -) Data:urldata]; [Self.view Addsubview:dataviewweb];

GifView.h

#import <UIKit/UIKit.h>#import <ImageIO/ImageIO.h>@interface  Gifview: UIView {    cgimagesourceref gif;     *gifproperties;    size_t index;    size_t count;     *timer;} -(ID) initWithFrame: (CGRect) frame FilePath: (NSString *) _filepath; -(ID) initWithFrame: (CGRect) frame data: (NSData *) _data; @end

gifview.m

#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] retain]; 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] retain]; 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= (ID)ref; Cfrelease (ref);}-(void) removefromsuperview{NSLog (@"Removefromsuperview");    [Timer invalidate]; Timer=Nil; [Super Removefromsuperview];}- (void) dealloc {NSLog (@"Dealloc");    Cfrelease (GIF);    [Gifproperties release]; [Super Dealloc];}@end

< four > Summary

1, through the Uiimageview display animation effect, in fact, the dynamic diagram has been split into a set of static graphs, put into the array, played in sequence from the array to remove. If the picture is played less than the memory is relatively small or more commonly used (such as the toolbar has been shown on the dynamic small icon), you can choose to use the imagenamed: Way to get the picture, but in this way to add in memory, use the end, do not release themselves, multiple playback of the animation will cause memory overflow problem. Therefore, for larger or frequently changed diagrams, when taking pictures, you can choose Imagewithcontentsoffile: the way to get pictures, optimize memory.

2, using UIWebView display pictures need to pay attention to the size of the display picture and UIWebView size settings, if only to display the dynamic picture, you can disable UIWebView scrolling. When the dynamic picture is displayed, even if the background of the motion diagram is transparent, the default display is white background, this time need to manually set the UIWebView transparency to achieve the effect of the dynamic picture background transparency.

3, third-party Gifview use is relatively simple, the class can be imported. However, the Gifview is MRC, so in an arc environment, the class needs to be identified.

4, Uiimageview and third-party gifview are through the timer to control the animation of the image simulation, playing time is set each frame of the time, so in the use of time, to try to move with the original length of time close, otherwise the animation effect will be somewhat strange. By UIWebView loading the GIF motion diagram will maintain the original frame rate, do not need to set again.

To learn more about the small partners, you can click to view the source code , run the test yourself.

Inquiries or technical exchanges, please join the official QQ Group: (452379712)

Jerry Education
Source:http://www.cnblogs.com/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

IOS play animated GIF pictures

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.