IPHONE GIF播放的方式

來源:互聯網
上載者:User

1.UIWebView 播放(無法控制周期)

.h

 @interface webViewController : UIViewController { 
    IBOutlet UIWebView *WebView; 
    IBOutlet UINavigationBar *Nav; 

@property (nonatomic, retain) IBOutlet UIWebView *WebView; 
@end 

.m

 - (void)viewDidLoad { 
    [super viewDidLoad]; 
    [WebView setOpaque:NO]; 
    [WebView setBackgroundColor:[UIColor clearColor]]; 
    Nav.topItem.title=@"Welcome"; 
    Nav.topItem.leftBarButtonItem=nil;   
    NSString* gifFileName = @"j_0002.gif"; 
    NSMutableString* htmlStr = [NSMutableString string]; 
    [htmlStr appendString:@"Hello Honey"]; 
    [htmlStr appendString:@"<p><img src=\""]; 
    [htmlStr appendFormat:@"%@",gifFileName]; 
    [htmlStr appendString:@"\" alt=\"picture\"/>"]; 
    [WebView loadHTMLString:htmlStr baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]]; 
} www.2cto.com

2.glgif提供的開原始碼實現(目前無法控制周期,需要自己再研究)


3.使用UIView的animation介面實現(GIF需要整理出每張圖片)

動畫可以用UIImageView,把動畫的每一幀做成圖片,轉換成UIImage儲存在animationImages數組中,調用- (void)startAnimating;就能看到動畫


4.把GIF的通過GIF的格式轉換成每張圖片,通過UIView的animation的介面實現(GIF的格式貌似還是有問題,有部分圖片的背景有問題)

view plain
- (NSMutableArray*)getImgArrayByName:(NSString*)name 

    //happybaby20 
    NSURL *threeUrl =[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"crad"  
                                                                            ofType:@"gif"]]; 
    NSData* data = [[NSData alloc]initWithContentsOfURL:threeUrl]; 
    NSMutableArray* array = nil; 
//  long word = ::SizeofResource(handle,hrsrc); 
//  char* lpBy = (char*)LoadResource(handle,hrsrc); 
    DWORD word = data.length; 
    BYTE* lpBy = (BYTE*)data.bytes; 
    //每張圖儲存的指標 需要釋放 
    BYTE* pByte[100] = {0}; 
    //每張圖的大小 
    DWORD nu[100] = {0}; 
    //圖片數量 
    int num = 0; 
    DWORD firstLocation = 0; 
    for(DWORD j=0;j<word;j++) 
    { 
        if(lpBy[j]==0x2c) 
        { 
            if(lpBy[j-1]==0x00) 
            {                
                if(num==0) 
                { 
                    firstLocation = j; 
                } 
                 
                if (num== 10) 
                { 
                    int sdf=nu[num]; 
                    int sdfadf=0; 
                } 
                 
                PGifImage nowImage = (PGifImage)&lpBy[j+1]; 
                 
                if(nowImage->Flag.a==0) 
                { 
                    DWORD number = 1+sizeof(GifImage); 
                    while(lpBy[j+number]!=0) 
                    { 
                        number = number+(DWORD)lpBy[j+number]+1; 
                    } 
                    number++; 
                    pByte[num] = new BYTE[number]; 
                    memset(pByte[num],0,number); 
                    for(DWORD n=0;n<number;n++) 
                    { 
                        *(BYTE*)(pByte[num]+n) = lpBy[j+n]; 
                    } 
                    nu[num] = number; 
                    j = j+number-1; 
                    num++; 
                } 
                else 
                { 
                    //int number = 1+ sizeof(GifImage) + 3*(int)floor(pow(2.0f,nowImage->Flag.d)); 
                     
                    DWORD number = 1+sizeof(GifImage)+1+3*(int)floor(pow(2.0f,nowImage->Flag.d)); 
                    while(lpBy[j+number] != 0) 
                    { 
                        number = number+lpBy[j+number]; 
                    } 
                    pByte[num] = new BYTE[number]; 
                    memset(pByte[num],0,number); 
                    for(int n=0;n<number;n++) 
                    { 
                        *(BYTE*)(pByte[num]+n) = lpBy[j+n]; 
                    } 
                    nu[num] = number; 
                    j = j+number-1; 
                    num++; 
                } 
                 
 
            } 
        } 
    } 
 
    NSArray *userPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentDirectory = [userPaths objectAtIndex:0]; 
     
    //數量上 
    if (num>0) 
    { 
        array = [[NSMutableArray alloc] initWithCapacity:num]; 
    } 
    for (int k=0; k<num; k++) 
    { 
        for(DWORD n=0;n<nu[k];n++) 
        { 
            lpBy[firstLocation+n] = *(BYTE*)(pByte[k]+n); 
        } 
         
        NSData* imgbuf = [[NSData alloc] initWithBytes:lpBy length:nu[k] + firstLocation]; 
 
        if (imgbuf) 
        { 
 
            UIImage* img = [[UIImage alloc]initWithData:imgbuf]; 
             
            NSData* imgdd = UIImagePNGRepresentation(img); 
             
            [imgdd writeToFile:[NSString stringWithFormat:@"%@/sdf%d.png",documentDirectory,k] atomically:NO]; 
             
            if (img) 
            { 
                [array addObject:img]; 
            } 
             
            [img release]; 
        } 
        [imgbuf release]; 
         
        delete pByte[k]; 
        pByte[k] = NULL; 
    } 
 
    return array; 

摘自:zltianhen的專欄

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.