Guest User

Untitled

a guest
Jul 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. class AccountSettings: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
  2.  
  3. @IBOutlet weak var profileImage: UIImageView!
  4. var imageRef: StorageReference{
  5. return Storage.storage().reference().child("profile_images")
  6. }
  7.  
  8. let cache = NSCache<NSString, UIImage>()
  9. var activityIndicator = UIActivityIndicatorView()
  10.  
  11. override func viewDidLoad() {
  12. activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
  13. activityIndicator.frame = CGRect(x: 0, y: 0, width: 46, height: 46)
  14. activityIndicator.hidesWhenStopped = true
  15.  
  16. profileImage.addSubview(activityIndicator)
  17. activityIndicator.startAnimating()
  18.  
  19.  
  20. if let image = cache.object(forKey: "(UserDefaults.standard.string(forKey: "unique_id")!).jpg" as NSString){
  21. profileImage.image = image
  22. }else{
  23. retrieveCurrentProfileImg()
  24. }
  25.  
  26. }
  27.  
  28. func retrieveCurrentProfileImg(){
  29. let downloadImageRef = imageRef.child("(UserDefaults.standard.string(forKey: "unique_id")!).jpg")
  30.  
  31. let downloadTask = downloadImageRef.getData(maxSize: 1024*1024*12) { (data, error) in
  32. if let data = data{
  33. let image = UIImage(data: data)
  34. self.profileImage.image = image
  35. self.cache.setObject(image!, forKey: "(UserDefaults.standard.string(forKey: "unique_id")!).jpg" as NSString)
  36. self.activityIndicator.stopAnimating()
  37. }
  38.  
  39. print(error ?? "No error")
  40.  
  41. }
  42. downloadTask.observe(.progress) { (snapshot) in
  43.  
  44. }
  45. downloadTask.resume()
  46.  
  47. }
Add Comment
Please, Sign In to add comment