I have a *.gif file that I want to show in a UIImageView. I have tried the library FLAnimatedImage, code below. Result is just a static image.
class LoginVC: UIViewController, UITextFieldDelegate {
#IBOutlet weak var img_popup: FLAnimatedImageView!
var img_popupraw: FLAnimatedImage = FLAnimatedImage(animatedGIFData: NSData(contentsOfFile: "ShitTalk_LoadingAnimation.gif"))
override func viewDidLoad() {
img_popup.animatedImage = img_popupraw
}
}
Open to any alternative ways to show animating gif directly from a gif file. I am using Swift.
I strongly recommend you to use SwiftGif.
Import the Gif.swift in your project and do the following:
// Returns an animated UIImage
let jeremyGif = UIImage.gifWithName("jeremy")
// Use the UIImage in your UIImageView
let imageView = UIImageView(image: jeremyGif)
In my case i'm using SDWebImage for manage all image in app. Inclusive have sd_animatedGIFNamed func
install pod 'SDWebImage'
//
// ViewController.swift
// Loader
//
// Created by Pawan Kumar on 28/09/17.
// Copyright © 2017 Pawan Kumar. All rights reserved.
//
import UIKit
import FLAnimatedImage
class ViewController: UIViewController {
#IBOutlet weak var animatedImageView: FLAnimatedImageView!
// #IBOutlet weak var checkImageView: FLAnimatedImageView!
override func viewDidLoad() {
super.viewDidLoad()
let url = Bundle.main.path(forResource: "Loader", ofType: "gif")
print(url!)
let data=NSData(contentsOfFile: url!)
//print(data)
let fff=FLAnimatedImage(gifData: data as Data?)
let imageView=FLAnimatedImageView()
imageView.animatedImage=fff
//
// imageView.frame=CGRect(x: animatedImageView.frame.minX, y: animatedImageView.frame.minY, width: animatedImageView.frame.width, height: animatedImageView.frame.height)
imageView.frame=CGRect(x: animatedImageView.frame.minX, y: animatedImageView.frame.minY, width: animatedImageView.frame.width, height: animatedImageView.frame.height)
print(imageView.currentFrameIndex)
imageView.clipsToBounds=true
self.view.addSubview(imageView)
// 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.
}
}
Create a view controller as shown in the code above.
Note:- while creating an imageview in story board, set the class of image view as FLanimatedImageView.
It will work.
Here Loader.gif is the GIF image.
Related
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.
I am a beginner so please bear with me. I am trying to load a gif onto one of my pages, and so far I have figured out how to do it on the initial screen, but I would like the gif to load on another page instead. I know I should move it out of the 'override func viewDidLoad() {}' function, but I don't know what to do afterwards. Could somebody please help me? Thank you!
Here is the code so far:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let imageData = NSData(contentsOfURL: NSBundle.mainBundle().URLForResource("fire", withExtension: "gif")!)
let imageGif = UIImage.gifWithData(imageData!)
let imageView = UIImageView(image: imageGif)
imageView.frame = CGRect(x: 0.0, y:0.0, width:414.0, height: 673.0)
view.addSubview(imageView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I'm trying to create my first game in apples xcode program using swift code but I've come across a problem with Arrays. Here's the code:
//
// ViewController.swift
// War
//
// Created by grayson seymour on 5/13/16.
// Copyright © 2016 SanGames. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var firstCardImageView: UIImageView!
#IBOutlet weak var secondCardImageView: UIImageView!
#IBOutlet weak var playRoundButton: UIButton!
#IBOutlet weak var backgroundImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.playRoundButton.setTitle("Play", forState: UIControlState.Normal)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func playRoundTapped(sender: UIButton) {
var cardNamesArray:[String] = ["card0", "card1", "card2", "card3", "card4", "card5", "card6", "card7", "card8", "card9"]
// Creates randomly generated number for first and second card
let randomCardFirst = Int(arc4random_uniform(10))
let randomCardSecond = Int(arc4random_uniform(10))
// checks to see what imageview to set for the cards
let firstCardString:String = self.cardNamesArray[randomCardFirst]
let secondCardString:String = self.cardNamesArray[randomCardSecond]
// sets randomly generated number to imageview for second and first card
self.firstCardImageView.image = UIImage(named: firstCardString)
self.secondCardImageView.image = UIImage(named: secondCardString)
}
}
I'm getting an error on line 39 & 40 or more specifically
let firstCardString:String = self.cardNamesArray[randomCardFirst]
let secondCardString:String = self.cardNamesArray[randomCardSecond]
The error I'm getting is
value of type View Controller has no member cardNamesArray
As I am very new to this language I have dabbled a little bit in the code to see if I could fix the problem but everything I do just creates another error. Any help would be very appreciated. Thanks!
Why do you call self before your variable (Because your class has no variable called cardNamesArray), this variable is inside your function (mean that it is not global), so shouldn't call self.
Replace
let firstCardString:String = self.cardNamesArray[randomCardFirst]
let secondCardString:String = self.cardNamesArray[randomCardSecond]
By
let firstCardString:String = cardNamesArray[randomCardFirst]
let secondCardString:String = cardNamesArray[randomCardSecond]
If you just want to use this cardNamesArray in your function, you can try this solution above.
If you want to use it multiple time inside your class, you can declare cardNamesArray as a global variable.
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))
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";