ios uiimageview 中的动画图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17274201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:03:40  来源:igfitidea点击:

Animate images in uiimageview

iosobjective-canimationuiimageview

提问by user2515868

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

如何为来自 Web 服务的图像制作动画。我得到了从 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];

回答by Vinodh

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];

回答by Abhishek Thapliyal

Swift

迅速

Assuming that imageView is added via storyboard

假设通过故事板添加 imageView

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()

回答by Faizal Nowshad KN

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

有一个完美的动画代码可以在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()
    }
}