10

如何为来自 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];
4

3 回答 3

16

从 Web 服务下载图像:

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];
于 2013-06-24T11:33:53.040 回答
2

得到了一个完美的动画代码 可以在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()
    }
}
于 2020-02-18T09:04:35.567 回答
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()
于 2017-04-21T12:50:08.350 回答