如何为来自Web服务的图像设置动画。我得到了代码来动画来自bundle的图像。如何从url加载图像
该代码附于
之下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];
答案 0 :(得分:15)
从网络服务下载图片:
NSData *imageData = [NSData dataWithContentsOfURL:"*Url from web service*"];
UIImage *imageOne = [UIImage imageWithData:imageData];
可能从Web服务下载所有图像并创建如下数组:
NSArray *imagesArray = [NSArray arrayWithObjects:imageOne...........,nil];
并使用少许修改:
UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
animatedImageView.animationImages = imagesArray;
animatedImageView.animationDuration = 1.0f;
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[self.view addSubview: animatedImageView];
答案 1 :(得分:1)
假设通过故事板添加imageView
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")!]
self.imgView.animationImages = self.imageArray
self.imgView.animationDuration = 1.0
self.imgView.animationRepeatCount = 0
self.imgView.startAnimating()
self.imgView.animationRepeatCount = 1 // IF Require once set this 1, 0 == infinite
或强>
self.imgView.stopAnimating()
答案 2 :(得分:0)
获得了完美的动画代码 可以在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()
}
}