Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在uiimageview中设置图像动画_Ios_Objective C_Animation_Uiimageview - Fatal编程技术网

Ios 在uiimageview中设置图像动画

Ios 在uiimageview中设置图像动画,ios,objective-c,animation,uiimageview,Ios,Objective C,Animation,Uiimageview,如何从web服务设置图像动画。我得到了从bundle中设置图像动画的代码。如何从url数组中加载图像 该代码附在下面 UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds]; animatedImageView.animationImages = [NSArray arrayWithObjects: [UIIma

如何从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];

要从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];
敏捷的 假设imageView是通过故事板添加的

初始化数组: 添加代码以在按钮动作或根据需要设置动画 停止动画

self.imgView.stopAnimating()

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

下载图像,创建一个数组并设置
animationImages
,这里有什么问题?有问题吗?似乎代码是好的,问题是…误导和错误的标签,错误的标题标记。只是一个旁注:确保下载没有在主线程上执行。没有问题。但下载需要时间。您必须耐心等待图像下载。是否可以以类似方式更改uibutton的背景图像请清楚地询问您的问题,您希望在单击按钮后更改,或者直接更改仅供参考,但是如果使用这种方法,图像过大,您的代码将使设备崩溃。保存所有已解码的图像数据是内存不是一个好主意,请进行更多的研究,找出如何解决此问题。在viewcontrol中自动更改imageview中图像的答案是“无”。
self.imgView.animationRepeatCount = 1 // IF Require once set this 1, 0 == infinite 
self.imgView.stopAnimating()
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()
    }
}