UIImageView.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (C) 2016 Inigo Valentin
  2. //
  3. // This file is part of the Gasteizko Margolariak IOS app.
  4. //
  5. // The Gasteizko Margolariak IOS app is free software: you can
  6. // redistribute it and/or modify it under the terms of the
  7. // GNU General Public License as published by the Free Software
  8. // Foundation, either version 3 of the License, or (at your
  9. // option) any later version.
  10. //
  11. // The Gasteizko Margolariak IOS app is distributed in the
  12. // hope that it will be useful, but WITHOUT ANY WARRANTY;
  13. // without even the implied warranty of MERCHANTABILITY or
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  15. // Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public
  18. // License along with the Gasteizko Margolariak IOS app.
  19. // If not, see <http://www.gnu.org/licenses/>.
  20. import Foundation
  21. import UIKit
  22. /**
  23. Extension of UIImageView to include custom utilities.
  24. */
  25. extension UIImageView {
  26. /**
  27. Sets an image in the UIImageView. If the image is downloaded, it will not download it again.
  28. If it isn't, it will download it.
  29. :param: localPath Path where the image is or will be.
  30. :param: remotePath Url of the image to download (if not cached).
  31. :return: 0 if the file was set form the local path, 1 if the file was downloaded, other for error.
  32. */
  33. func setImage(localPath: String, remotePath: String){
  34. //Check if the local image exists.
  35. let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
  36. let url = NSURL(fileURLWithPath: path)
  37. let filePath = url.appendingPathComponent(localPath)?.path
  38. let fileManager = FileManager.default
  39. if fileManager.fileExists(atPath: filePath!) {
  40. //Set image
  41. do{
  42. //let iurl = URL(string: filePath!)
  43. let idata = try Data(contentsOf: URL(fileURLWithPath: filePath!))
  44. self.image = UIImage(data: idata as Data)
  45. }
  46. catch (let error){
  47. NSLog(":IMAGE:ERROR: Error setting image: \(error.localizedDescription)")
  48. return
  49. }
  50. return
  51. } else {
  52. NSLog(":IMAGE:LOG: Downloading image from \(remotePath)")
  53. //Create required directories
  54. var fileFolderUrl = URL(fileURLWithPath: filePath!)
  55. fileFolderUrl.deleteLastPathComponent()
  56. do{
  57. try fileManager.createDirectory(at: fileFolderUrl, withIntermediateDirectories: true, attributes: nil)
  58. }
  59. catch(let error){
  60. NSLog(":IMAGE:ERROR: Error creating directories at \(fileFolderUrl.path): \(error.localizedDescription)")
  61. }
  62. // Create destination URL
  63. let destinationFileUrl = URL(fileURLWithPath: filePath!)
  64. //Create URL to the source file you want to download
  65. let fileURL = URL(string: remotePath)
  66. let sessionConfig = URLSessionConfiguration.default
  67. let session = URLSession(configuration: sessionConfig)
  68. let request = URLRequest(url:fileURL!)
  69. let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
  70. if let tempLocalUrl = tempLocalUrl, error == nil {
  71. // Success
  72. if let statusCode = (response as? HTTPURLResponse)?.statusCode {
  73. // TODO something.
  74. }
  75. do {
  76. try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
  77. } catch (let writeError) {
  78. NSLog(":IMAGE:ERROR: Error creating a file \(destinationFileUrl) : \(writeError)")
  79. }
  80. // Set the image
  81. do{
  82. let idata = try Data(contentsOf: URL(fileURLWithPath: filePath!))
  83. self.image = UIImage(data: idata as Data)
  84. }
  85. catch (let error){
  86. NSLog(":IMAGE:ERROR: Error setting image: \(error.localizedDescription)")
  87. }
  88. } else {
  89. NSLog(":IMAGE:ERROR: Error took place while downloading a file.");
  90. }
  91. }
  92. task.resume()
  93. return
  94. }
  95. }
  96. }