我一直试图在Swift中为GIF制作动画,但它只是不起作用。我想我找到了办法,但是有错误,请帮助。
以下是代码:
@IBOutlet var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// (Here is the error it states "Cannot assign a value of type '[UIImage?]' to a value of type '[UIImage]?'")
imageView.animationImages = [
UIImage(named: "logo1.jpg"),
UIImage(named: "logo2.jpeg"),
UIImage(named: "logo3.png")
]
imageView.animationDuration = 3
imageView.startAnimating()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
// ...
}
答案 0 :(得分:1)
您可以直接运行gif文件,请点击以下链接;
https://github.com/kaishin/gifu
How to load GIF image in Swift?
就代码中的问题而言,正如'Leo Dabus'指出的那样,你是不是在解包UIImage实例。尝试使用以下修改的相同代码。
imageView.animationImages = [
UIImage(named: "logo1.jpg")!, //File Extension would not be required if .png
UIImage(named: "logo2.jpeg")!, //File Extension would not be required if .png
UIImage(named: "logo3.png")!
]
imageView.animationDuration = 3
imageView.startAnimating()
答案 1 :(得分:0)
您只需要打开UIImage(命名:)方法返回的可选UIImage。试试这样:
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imageView.animationImages = [
UIImage(named: "logo1")!,
UIImage(named: "logo2")!,
UIImage(named: "logo3")!]
imageView.animationDuration = 3
imageView.startAnimating()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 2 :(得分:0)
我为你创造了一个例子。
这是代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imageView.animationImages = [
UIImage(named: "1")!,
UIImage(named: "2")!,
UIImage(named: "3")!,
UIImage(named: "4")!,
UIImage(named: "5")!,
UIImage(named: "6")!,
UIImage(named: "7")!,
UIImage(named: "8")!,
UIImage(named: "9")!,
UIImage(named: "10")!,
UIImage(named: "11")!,
UIImage(named: "12")!
]
imageView.animationDuration = 3
imageView.startAnimating()
}
}
您可以看到结果HERE。
HERE是示例项目。