Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display an activity indicator with text on iOS 8 with Swift?

Tags:

swift

ios8

I wanna show, programmatically, an activity indicator with text, like the one in the Photos app (after editing and saving a picture). How can I do this?

enter image description here

like image 742
rodrigoalvesvieira Avatar asked Feb 28 '15 19:02

rodrigoalvesvieira


People also ask

How do I display activity indicator in Swift?

Select the Assistant Editor and make sure the ViewController. swift is visible. Ctrl and drag from the Start Button to the ViewController class and create the following Action. Ctrl and drag from the Stop Button to the ViewController class and create the following Action.

What is UIActivityIndicatorView?

A view that shows that a task is in progress.

How do I hide activity indicator in Swift?

Select the Activity Indicator in Storyboard, then select property "Hides when stopped". This way you don't have to hide it, just start or stop the animation, and the Activity Indicator will show and hide automatically. Of course, you still have to add the code to start and stop the animation to buttons.


1 Answers

Xcode 9.0 • Swift 4.0


import UIKit class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {     @IBOutlet weak var imageView: UIImageView!     @IBOutlet weak var filterButton: UIButton!     @IBOutlet weak var saveButton: UIButton!     let destinationUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!         .appendingPathComponent("filteredImage.png")     let imagePicker = UIImagePickerController()     let messageFrame = UIView()     var activityIndicator = UIActivityIndicatorView()     var strLabel = UILabel()     let effectView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))     func activityIndicator(_ title: String) {         strLabel.removeFromSuperview()         activityIndicator.removeFromSuperview()         effectView.removeFromSuperview()         strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 160, height: 46))         strLabel.text = title         strLabel.font = .systemFont(ofSize: 14, weight: .medium)         strLabel.textColor = UIColor(white: 0.9, alpha: 0.7)         effectView.frame = CGRect(x: view.frame.midX - strLabel.frame.width/2, y: view.frame.midY - strLabel.frame.height/2 , width: 160, height: 46)         effectView.layer.cornerRadius = 15         effectView.layer.masksToBounds = true         activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .white)         activityIndicator.frame = CGRect(x: 0, y: 0, width: 46, height: 46)         activityIndicator.startAnimating()         effectView.contentView.addSubview(activityIndicator)         effectView.contentView.addSubview(strLabel)         view.addSubview(effectView)     }     func saveImage() {         do {             try imageView.image?.data?.write(to: destinationUrl, options: .atomic)             print("file saved")         } catch {             print(error)         }     }             func applyFilterToImage() {         imageView.image = imageView.image?.applying(contrast: 1.5)     }     override func viewDidLoad() {         super.viewDidLoad()         guard let url = URL(string: "https://upload.wikimedia.org/wikipedia/commons/a/a8/VST_images_the_Lagoon_Nebula.jpg"), let data = try? Data(contentsOf: url), let image = UIImage(data: data) else { return }         view.backgroundColor = UIColor(white: 0, alpha: 1)         imageView.image = image     }     @IBAction func startSavingImage(_ sender: AnyObject) {         saveButton.isEnabled = false         filterButton.isEnabled = false         activityIndicator("Saving Image")         DispatchQueue.main.async {             self.saveImage()             DispatchQueue.main.async {                 self.effectView.removeFromSuperview()                 self.saveButton.isEnabled = true                 self.filterButton.isEnabled = true             }         }     }     @IBAction func filterAction(_ sender: AnyObject) {         filterButton.isEnabled = false         saveButton.isEnabled = false         activityIndicator("Applying Filter")         DispatchQueue.main.async {             self.applyFilterToImage()             DispatchQueue.main.async {                 self.effectView.removeFromSuperview()                 self.filterButton.isEnabled = true                 self.saveButton.isEnabled = true             }         }     }     @IBAction func cameraAction(_ sender: AnyObject) {         if UIImagePickerController.isSourceTypeAvailable(.camera) {             imagePicker.delegate = self             imagePicker.sourceType = .camera             present(imagePicker, animated: true)         }     }     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [AnyHashable: Any]!) {         dismiss(animated: true, completion: nil)         imageView.image = image     } } 

extension Data {     var image: UIImage? { return UIImage(data: self) } } 

extension UIImage {     var data: Data? { return UIImagePNGRepresentation(self) }     func applying(contrast value: NSNumber) -> UIImage? {         guard let ciImage = CIImage(image: self)?.applyingFilter("CIColorControls", withInputParameters: [kCIInputContrastKey: value]) else { return nil }         UIGraphicsBeginImageContextWithOptions(size, false, scale)         defer { UIGraphicsEndImageContext() }         UIImage(ciImage: ciImage).draw(in: CGRect(origin: .zero, size: size))         return UIGraphicsGetImageFromCurrentImageContext()     } } 

like image 131
Leo Dabus Avatar answered Sep 21 '22 06:09

Leo Dabus