Animate images in uiimageview

2019-01-18 12:31发布

问题:

How to animate the images from web service. I got the code to animate the images from bundle.How to load the images from the url an array

That code is attached below

UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
animatedImageView.animationImages = [NSArray arrayWithObjects:    
                               [UIImage imageNamed:@"image1.gif"],
                               [UIImage imageNamed:@"image2.gif"],
                               [UIImage imageNamed:@"image3.gif"],
                               [UIImage imageNamed:@"image4.gif"], nil];
animatedImageView.animationDuration = 1.0f;
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[self.view addSubview: animatedImageView];

回答1:

To download images from webservice ;

NSData *imageData = [NSData dataWithContentsOfURL:"*Url from web service*"];
 UIImage *imageOne = [UIImage imageWithData:imageData];

likely download all images from web service and create an array like

NSArray *imagesArray = [NSArray arrayWithObjects:imageOne...........,nil];

and use with little modification

UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
animatedImageView.animationImages = imagesArray;
animatedImageView.animationDuration = 1.0f;
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[self.view addSubview: animatedImageView];


回答2:

Swift

Assuming that imageView is added via storyboard

Intialize array:

self.imageArray = [UIImage(named:"Download_060.png")!,UIImage(named:"Download_061.png")!,UIImage(named:"Download_062.png")!,
                       UIImage(named:"Download_063.png")!,UIImage(named:"Download_064.png")!,UIImage(named:"Download_065.png")!,
                       UIImage(named:"Download_066.png")!,UIImage(named:"Download_067.png")!]

Add Code to animate on button action or as per required

 self.imgView.animationImages = self.imageArray
        self.imgView.animationDuration = 1.0
        self.imgView.animationRepeatCount = 0
        self.imgView.startAnimating()

Stop Animation

self.imgView.animationRepeatCount = 1 // IF Require once set this 1, 0 == infinite 

OR

self.imgView.stopAnimating()