Connecting to a specific UIImageView instead of initial View - ios

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.
}
}

Related

Take screenshot of whole WebView in swift 4 iOS

I really hope you can help me.
In my project I have a WebView opening a specific page and a button, what I need to do is taking a screenshot of the whole web page and export it as image or pdf when user press the button.
Here is what I have in my ViewCnotroller:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var WebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL (string: "https://www.google.com")
QuizView.loadRequest(URLRequest(url: url!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func ShotBtn(_ sender: Any) {
let activityVC = UIActivityViewController(activityItems: [self.WebView.image], applicationActivities: nil)
}
}
Now I know that value of type UIWebView has no member image, but the only example I found was using an UIImage insted WebView.
I saw this answer Get UIWebView content and convert it to image but I was looking for something in swft 4
How can I do it?
Thankyou

UIWebView delegates don't fire in a custom class

I spent around 5 hours searching on StackOverFlow about how to implement this and I still not found a solution yet!
I have created a new iOS Swift app just to explain you what exactly I am trying to resolve.
First, I have a UIButton on main storyboard and when you click on it, it creates a new object from class WebViewAsPopup which creates programmatically a new WebView and a background as UIView with alpha 0.5, add some styling and then it add them to the main view controller ViewController as a popup.
Secondly, I couldn't set a delegate to those WebView's to handle UIWebViewDelegate and listen to both webViewDidStartLoad and webViewDidFinishLoad
Here's my code:
ViewController.swift
//
// ViewController.swift
// MySimpleApp
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var showPopupBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func showPopupTapped(_ sender: Any) {
let popup = WebViewAsPopup()
popup.showWebViewPopup(on: self)
}
}
WebViewAsPopup.swift
//
// WebViewAsPopup.swift
// MySimpleApp
//
import WebKit
class WebViewAsPopup: NSObject, UIWebViewDelegate {
func showWebViewPopup(on controller: UIViewController) {
// Popup background
let bg = UIView()
bg.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
bg.layer.backgroundColor = UIColor.black.withAlphaComponent(0.5).cgColor
// Webview sizing
let width = UIScreen.main.bounds.width - 60
let height = UIScreen.main.bounds.height - 200
let x = UIScreen.main.bounds.width/2 - width/2
let y = UIScreen.main.bounds.height/2 - height/2
// Webview stuff
let webView = UIWebView()
webView.frame = CGRect(x: x, y: y, width: width, height: height)
let url = URL(string: "https://google.com")
let request = URLRequest(url: url!)
// webView.delegate = self << it crash here
webView.loadRequest(request)
// Styling webview popup
webView.layer.borderWidth = 1
webView.layer.borderColor = UIColor.black.cgColor
webView.layer.masksToBounds = true
webView.layer.cornerRadius = 10
// Add bg then webview to main view controller
controller.view.addSubview(bg)
controller.view.addSubview(webView)
}
func webViewDidStartLoad(_ webView: UIWebView)
{
print("#webViewDidStartLoad!") // << it doesn't fire :(
}
func webViewDidFinishLoad(_ webView: UIWebView)
{
print("#webViewDidFinishLoad!") // << it doesn't fire :(
}
}
As I noted in my comments, you need to save the popup object somewhere in order to retain it. If not (as in your example code), then it will be deallocated as soon as the action method showPopupTapped(_:) is completed.
Try this:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var showPopupBtn: UIButton!
let popup = WebViewAsPopup()
#IBAction func showPopupTapped(_ sender: Any) {
self.popup.showWebViewPopup(on: self)
}
}
Bonus Note:
If you're wondering why you don't need to do the same to the background view and web view in your popup object, that's because views automatically retain their subviews.
So, once you add your background view and web view as subviews to a parent view, their retain count goes up and will survive their current scope.

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.

Unable to show a *.gif file in a UIImageView in swift

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.

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