用UIImageVeiw來實現gif動態圖

所謂gif動態圖就是一張張連拍的圖片連續快速放映所形成的效果圖片。而ios中並沒有這種格式的圖片,但是我們可以通過UIImageView來實現。

1.將你獲得的圖片資源放在Supporting Files中

2.實現

我們在UIImageView中有下面兩個屬性

@property(nonatomic,retain)UIImage *image;    

@property(nonatomic,copy)NSArray *animationImages;           // The array must contain UIImages. Setting hides the single image. default is nil

我們用的最多的是第一個屬性,就是讓它顯示一張圖片。第二個屬性是給它設置一個動畫播放的數組,裏面存的是圖片(不是圖片的名字),設置了,會隱藏第一張圖。

- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view.

    //創建一個imageView,居中,我選的圖片大小是31*52

    UIImageView *imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(320/2-31/2,self.view.frame.size.width/2-52/2,31, 52)];

    //創建一個數組,用來存圖片,我的由10張圖生成

    NSMutableArray *imageArr=[NSMutableArrayarray];

    //將圖片存入數組中

    for (int i=0; i<10; i++) {

        NSMutableString *imageName=[NSMutableStringstringWithFormat:@"%d.png",i];

        UIImage *img=[UIImageimageNamed:imageName];

        NSLog(@"imageName=%@",imageName);

        [imageArr addObject:img];

    }

    //設置播放數組

    [imageView setAnimationImages:imageArr];

    //設置播放持續的時間,多久將圖片播放一輪

    [imageView setAnimationDuration:1.0];

    //開始播放,動態圖效果就出現了

    [imageView startAnimating];

    [self.view addSubview:imageView];

    [imageView release];

    

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章