Animate Gif in Swift - ios

I've been trying to animate a GIF in Swift but it just doesn't work. I think I found a way but there's an error, please help.
Here's the code:
#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.
// ...
}

You may run gif files directly, follow the links below;
https://github.com/kaishin/gifu
How to load GIF image in Swift?
As far as the issue in you code is, as already 'Leo Dabus' pointed out, is you are not un-wrapping the UIImage instances. Try same code with the following modifications.
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()

You just need to unwrap the optional UIImage returned by UIImage(named:) method. Try like this:
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.
}
}

I have created one example for you.
Here is code:
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()
}
}
You can see Result HERE.
And HERE is sample project.

Related

Setting an image as Navigation Bar title

I'm trying to set an image as the title of a navigation bar in swift using the code below. The code builds successfully, but the image does not display. Any advice would be appreciated.
import UIKit
class FirstViewController: UIViewController {
#IBOutlet weak var navigationBar: UINavigationItem!
var convoImage: UIImage!
var convoImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.convoImage = UIImage(contentsOfFile: "conversation.png")
self.convoImageView = UIImageView(image: self.convoImage)
self.navigationBar.titleView = self.convoImageView
}
}
Set the navigationItem's titleView.
let image = UIImage(named: "image_name.png")
self.navigationItem.titleView = UIImageView(image: image)
In your code replace below line:
self.convoImage = UIImage(contentsOfFile: "conversation.png")
with
self.convoImage = UIImage(named: "conversation.png")
You should set convoImageView frame .
import UIKit
class FirstViewController: UIViewController {
#IBOutlet weak var navigationBar: UINavigationItem!
var convoImage: UIImage!
var convoImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.convoImage = UIImage(named: "conversation.png")
self.convoImageView = UIImageView(image: self.convoImage)
self.convoImageView.frame = CGRectMake(0,0,720,100)
self.navigationBar.titleView = self.convoImageView
}
}

Can't understand why UISegmentedControl is returning nil

Just messing around with a simple iOS app in Swift that displays an image view and segmented control underneath with 3 options. I'm just trying to get the image to swap out based on which option is chosen from the segmented control. I have this simple code so far:
class ViewController: UIViewController {
var gallery = ["apple", "banana", "coconut"]
#IBOutlet weak var imageView: UIImageView!
#IBAction func changeSegment(sender: AnyObject) {
let segmentedControl = sender as? UISegmentedControl
if let index = segmentedControl?.selectedSegmentIndex {
imageView.image = UIImage(named: gallery[index])
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I keep running into an error that prints out "(lldb)". It looks like it hits a breakpoint at:
let segmentedControl = sender as? UISegmentedControl
not sure why. Any and all help is greatly appreciated!
You have a breakpoint on the side of your code. I would clean your file and run.

Error when trying to set the iOS-application's background

Before I upgraded XCode to version 7.3.1 (7D1014), the following code snippet changed background of my application.
class InitialViewController: UIViewController {
#IBOutlet weak var startImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "FogCity")!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Now I get following error:
This image exists:
The "print" function displays following:
Please help me!
Your code is correct, I tested it. I refer to this line of code:
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "FogCity")!)
What you have to do is to check the name of the image "FogCity" to exactly match.
Where is its 1x image? First put this image too, then try again...
Use image name with extension like .png or .jpeg
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "FogCity.png")!)

Error in UIImageView Animation

Absolute Swift beginner here.
I have been trying to get a simple set of png's to animate.
Here is what I have:
Any help would be much appreciated.
Thanks
#import <UIKit/UIKit.h>
class ViewController: UIViewController {
#IBOutlet var myImageView : UIImageView
#IBOutlet var animationBtn : UIButton
var imageList = UIImage[]()
#IBAction func animationBtnClicked(sender : AnyObject) {
startAnimation()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
for i in 1...13
{
let imageName = "\(i)"
imageList += UIImage(named: imageName)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func startAnimation() -> Void
{
if !myImageView.isAnimating()
{
myImageView.animationImages = imageList
myImageView.startAnimating()
animationBtn.setTitle("Stop Animation", forState: UIControlState.Normal)
} else
{
myImageView.stopAnimating()
myImageView.image = UIImage(named:"bomb.jpg")
animationBtn.setTitle("Start Animation", forState: UIControlState.Normal)
}
}
}
Here is the adjusted code if it helps anyone.
Cheers
import UIKit
class ViewController: UIViewController {
#IBOutlet var myImageView: UIImageView!
#IBOutlet var animationBtn: UIButton!
var imageList = [UIImage]()
#IBAction func animationBtnClicked(sender: AnyObject) {
startAnimation()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
for i in 1...99
{
let imageName = "\(i)"
imageList.append(UIImage(named: imageName)!)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func startAnimation() -> Void
{
myImageView.animationImages = imageList
myImageView.startAnimating()
}
}
Those errors messages shows the exact issue.
Issue 1
For fixing has no initializers, you need implement at-least one init function in your class.
Issue 2
For fixing IBOutlet has non-optional, you need to change your IBOutlet variables to optionals.
#IBOutlet var myImageView : UIImageView!
#IBOutlet var animationBtn : UIButton!
Issue 3
For fixing that [UIImage] is not identical to UInt8. The += can't be used to append a single UIImage to [UIImage]
You need to change:
imageList += UIImage(named: imageName)
to
imageList.append(UIImage(named: imageName))

how to change uiimage's image on button click in swift?

I'm trying change an uiimage control's containing image by clicking a button in swift. For this, i wrote a simple a "guess how many fingers i'm holding up" app. I've read some stackoverflow articles but couldn't solve the problem. Below you can see my code and my ui. How can i change change image on click?
Here's my code:
import UIKit
class ViewController: UIViewController {
#IBOutlet var myImageView: UIImageView!
#IBOutlet var inputField: UITextField!
#IBAction func clickedGuessButtonAction(sender: AnyObject) {
println("Guess button clicked")
var randomX = Int(arc4random()%6)
println("randomX = \(randomX)")
var guess = inputField.text.toInt();
let image0 = UIImage(named: "images/qm.jpg")
let image1 = UIImage(named: "images/tick.jpg")
let image2 = UIImage(named: "images/cross.jpg")
if((inputField.text) != nil){
if(guess == randomX){
println("correct")
myImageView.image=UIImage(named: "tick.jpg") // THIS IS NOT WORKING
myImageView.image=image1 // THIS IS NOT WORKING TOO
inputField.resignFirstResponder();// hides keyboard
}
else
{
println("wrong")
myImageView.image=UIImage(named: "cross.jpg")
inputField.resignFirstResponder();//hides keyboard
}
}
else{
println("invalid input. requires integer only")
inputField.resignFirstResponder();// hides keyboard
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
And here's my UI:
You don't need to specify either the asset catalog's name or the image file's extension. Try removing those. For example,
myImageView.image = UIImage(named: "tick")
It may depend on the version of Xcode you are using. For me the following worked:
ImageView.image = UIImage(named: "Elephants.jpg")
In Swift:
myImageView = "nameOfImage";

Resources