Class: Tgios::ImageLoader

Inherits:
BindingBase show all
Defined in:
lib/tgios/image_loader.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BindingBase

#dealloc, #hook, #prepareForRelease, #unhook

Constructor Details

#initialize(url) ⇒ ImageLoader

Returns a new instance of ImageLoader.



4
5
6
7
8
# File 'lib/tgios/image_loader.rb', line 4

def initialize(url)
  super
  @url = url.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
  @events = {}
end

Class Method Details

.base_pathObject



38
39
40
# File 'lib/tgios/image_loader.rb', line 38

def self.base_path
  @base_path ||= "#{NSTemporaryDirectory()}web/"
end

.clear_filesObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tgios/image_loader.rb', line 60

def self.clear_files
  fm = NSFileManager.defaultManager
  files = fm.contentsOfDirectoryAtPath(self.base_path, error:nil)
  files.each do |filename|
    fm.removeItemAtPath("#{self.base_path}#{filename}", error: nil)
  end if files.present?
  cache_path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true).last
  fm.contentsOfDirectoryAtPath(cache_path, error: nil).each do |filename|
    fm.removeItemAtPath("#{cache_path}/#{filename}", error: nil)
  end
end

.load_url(url, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/tgios/image_loader.rb', line 72

def self.load_url(url, &block)
  if url.nil?
    block.call(nil, nil)
  else
    image_loader = ImageLoader.new(url)
    image_loader.on(:image_loaded) do |image, success|
      block.call(image, success)
      image_loader.prepareForRelease
    end
    image_loader.load
  end
end

.scale_to(image, scale = screen_scale) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/tgios/image_loader.rb', line 85

def self.scale_to(image, scale=screen_scale)
  if image.is_a?(UIImage) && image.scale != scale
    UIImage.imageWithCGImage(image.CGImage, scale: scale, orientation: image.imageOrientation)
  else
    image
  end
end

.screen_scaleObject



93
94
95
# File 'lib/tgios/image_loader.rb', line 93

def self.screen_scale
  UIScreen.mainScreen.scale
end

Instance Method Details

#file_pathObject



50
51
52
53
54
55
56
57
58
# File 'lib/tgios/image_loader.rb', line 50

def file_path
  @file_path ||= (
  NSFileManager.defaultManager.createDirectoryAtPath(self.class.base_path,
                                                     withIntermediateDirectories: true,
                                                     attributes: nil,
                                                     error: nil)
  "#{self.class.base_path}#{self.filename}"
  )
end

#filenameObject



42
43
44
45
46
47
48
# File 'lib/tgios/image_loader.rb', line 42

def filename
  @filename ||= (
  nsurl = NSURL.URLWithString(@url)
  path = "#{nsurl.host}#{nsurl.path}"
  CGI.escape(path)
  )
end

#get_imageObject



28
29
30
31
# File 'lib/tgios/image_loader.rb', line 28

def get_image
  data = NSData.dataWithContentsOfFile(file_path)
  data.uiimage(self.class.screen_scale) unless data.nil?
end

#loadObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tgios/image_loader.rb', line 14

def load
  image = get_image
  if image.nil?
    AFMotion::Image.get(@url) do |result|
      image = result.object
      image = self.class.scale_to(image, self.class.screen_scale)
      @events[:image_loaded].call(image, result.success?) unless @events.nil? || @events[:image_loaded].nil?
      save_image(image) if image.is_a?(UIImage)
    end
  else
    @events[:image_loaded].call(image, true) unless @events.nil? || @events[:image_loaded].nil?
  end
end

#on(event, &block) ⇒ Object



10
11
12
# File 'lib/tgios/image_loader.rb', line 10

def on(event, &block)
  @events[event]=block.weak!
end

#onPrepareForReleaseObject



98
99
100
# File 'lib/tgios/image_loader.rb', line 98

def onPrepareForRelease
  @events = nil
end

#save_image(image) ⇒ Object



33
34
35
36
# File 'lib/tgios/image_loader.rb', line 33

def save_image(image)

  NSFileManager.defaultManager.createFileAtPath(self.file_path, contents: UIImageJPEGRepresentation(image, 0.95), attributes:nil)
end