Animate images in uiimageview
Asked Answered
Y

3

11

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];
Yalta answered 24/6, 2013 at 11:18 Comment(4)
Download the images, create an array and set animationImages, what is the problem here ?Displacement
any problem? seems the code is goodPeterson
And the problem is...Eugenol
Misleading and bad tagging, wrong title - flagged.Rexanna
O
16

To download images from web service:

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];
Obtuse answered 24/6, 2013 at 11:33 Comment(6)
Just a side note: Make sure the downloading is not performed on the main thread.Aurelioaurelius
no issues. But time takes for download . you have to wait patiently till images get downloadedObtuse
Is it possible to change the background image of the uibutton in similar wayYalta
please ask your question clearly , you want to change after a button click or directlyObtuse
Just an FYI, but your code will crash the device if the images are too large using this approach. Holding all decoded image data is memory is not a good idea, do some more research to find out how to fix that.Inequitable
Crashes for me too. Needs a better solutionEngrossment
S
2

Got a Perfect code for animation can automatically change images in image view in swift4

class ViewController: UIViewController {

var imgarray = [UIImage(named:"f1.png")!, 
                UIImage(named:"f2.png")!, 
                UIImage(named:"f3.png")!,  
                UIImage(named:"f4.png")!,
                UIImage(named:"f5.png")!, 
                UIImage(named:"index1.png")!,
                UIImage(named:"index2.png")!, 
                UIImage(named:"index3.png")!]

@IBOutlet weak var imgview: UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()

    self.imgview.animationImages = imgarray
    self.imgview.animationDuration = 10.0
    self.imgview.animationRepeatCount = 0
    self.imgview.startAnimating()
    }
}
Striate answered 18/2, 2020 at 9:4 Comment(1)
Nothing its the answer To change images in a imageview automatically in a viewcontrol.Striate
E
1

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()
Estes answered 21/4, 2017 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.