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 interface    uiimageview *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 (int i=1; i<7; i++) {        UIImage *image = [UIImage imagenamed:[nsstring stringwithformat:@ "Clock%02d.png", i]];< C7/>[imgarray addobject:image];    }    Assign an array of UIImage to the animated image array    imageview.animationimages = Imgarray;    Sets the length of time to perform a full animation    imageview.animationduration = 6*0.15;    Animation repeats (0 for repeat)    imageview.animationrepeatcount = 0;    Start playing animation    [ImageView startanimating];    Stop play animation  -(void) stopanimating;//determine if animation is being performed  -(BOOL) isanimating;

< two > use UIWebView to display animated images

    Get the path to the picture    nsstring *path = [[NSBundle Mainbundle] pathforresource:@ "Happy" oftype:@ "gif"];    Convert the picture to NSData    nsdata *gifdata = [NSData Datawithcontentsoffile:path];    Create a WebView, add to the interface    uiwebview *webview = [[UIWebView alloc] Initwithframe:cgrectmake (0, ($), $)];    [Self.view Addsubview:webview];    Automatic sizing    webview.scalespagetofit = YES;    No scrolling webView.scrollView.scrollEnabled = no;    Set the transparency effect    Webview.backgroundcolor = [Uicolor clearcolor];webview.opaque = 0;    Load 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: Display a local GIF picture//Convert picture to NSData data nsdata *localdata = [NSData datawithcontentsoffil    E:[[nsbundle Mainbundle] pathforresource:@ "bird" oftype:@ "gif"];    Create a third-party view display picture Gifview *dataview = [[Gifview alloc] Initwithframe:cgrectmake (0, (+), +) Data:localdata]; [Self.view addsubview:dataview];//Mode II: Show local GIF picture//Get picture path nsstring *path = [[NSBundle mainbundle] pathforresource:@ "CA  T "oftype:@" GIF "];//create a third-party view display picture Gifview *dataview2 = [[Gifview alloc] Initwithframe:cgrectmake (200, 300, 150, 100)    Filepath:path]; [Self.view addsubview:dataview2];//Mode III: Display GIF picture obtained from the network//network picture NSData *urldata = [NSData datawithcontentsofurl:[nsurl    urlwithstring:@ "Http://pic19.nipic.com/20120222/8072717_124734762000_2.gif"];    Create a third-party view display picture Gifview *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;    nsdictionary *gifproperties;    size_t index;    size_t count;    Nstimer *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> @implementation gifview-(ID) initWithFrame: (CGRect) frame    FilePath: (NSString *) _filepath{self = [super Initwithframe:frame]; if (self) {gifproperties = [[Nsdictionary dictionarywithobject:[nsdictionary Dictionarywithobject:[nsnumbe R Numberwithint:0] Forkey: (NSString *) Kcgimagepropertygifloopcount] fo        Rkey: (NSString *) kcgimagepropertygifdictionary] retain];        GIF = Cgimagesourcecreatewithurl ((cfurlref) [Nsurl Fileurlwithpath:_filepath], (cfdictionaryref) gifProperties);        Count =cgimagesourcegetcount (GIF);        Timer = [Nstimer scheduledtimerwithtimeinterval:0.12 target:self selector: @selector (play) Userinfo:nil Repeats:yes];    [Timer fire]; } return self;}    -(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.12 target:self selector: @selector (play) Userinfo:nil Repeats:yes];    [Timer fire]; } return self;}    -(void) play{index + +;    index = Index%count;    Cgimageref ref = 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://blog.csdn.net/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and CSDN Common, welcome reprint, but without the author's consent 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.