Class: MotionAL::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/motional/asset.rb

Overview

A wrapper of ALAsset class.

An ALAsset object represents a photo or a video managed by the Photo application.
Assets can have multiple representations, for example a photo which was captured in RAW and JPG. Different representations of the same asset may have different dimensions.

And added some convinience methods.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(al_asset) ⇒ Asset



17
18
19
# File 'lib/motional/asset.rb', line 17

def initialize(al_asset)
  @al_asset = al_asset
end

Instance Attribute Details

#al_assetObject (readonly)

An instance of ALAsset.



14
15
16
# File 'lib/motional/asset.rb', line 14

def al_asset
  @al_asset
end

Class Method Details

.create(source, metadata = nil) {|asset, error| ... } ⇒ nil

Note:

use ‘MotionAL::Asset.video_compatible?(video_path_url)` before creating video.

Create an asset.

Examples:

MotionAL::Asset.create(data, meta) do |asset, error|
  # asynchronous if a block given
  p asset.url.absoluteString
end

MotionAL::Asset.create(data, meta)

if MotionAL::Asset.video_compatible?(video_path_url)
  MotionAL::Asset.create(data, meta)
else
  p "This video contained incompatible data."
end

Yields:

  • (asset, error)

Yield Parameters:



51
52
53
54
55
56
57
58
59
# File 'lib/motional/asset.rb', line 51

def self.create(source,  = nil, &block)
  if source.kind_of?(NSData)
    self.create_by_image_data(source, , block)
  elsif source.kind_of?(NSURL)
    self.create_by_video_path(source, block)
  else
    self.create_by_cg_image(source, , block)
  end
end

.find_all(options = {}) {|asset, error| ... } ⇒ nil Also known as: each

Find and enumerate assets by options.

Examples:

MotionAL::Asset.find_all do |asset, error|
  # asynchronous if a block given
  p asset.url.absoluteString
end

MotionAL::find_by_name('MyAppAlbum') do |group|
  MotionAL::Asset.find_all(group: group, order: :desc, filter: :photo) do |asset|
    p asset.url.absoluteString
  end
end

indexset = NSMutableIndexSet.indexSetWithIndexesInRange(1..3)
MotionAL::Asset.find_all(indexset: indexset, order: :desc) do |asset|
  p asset.url.absoluteString
end

Options Hash (options):

  • :group (MotionAL::Group)

    Default is the Camera Roll.

  • :filter (Symbol)

    :all, :photo or :video

  • :order (Symbol)

    :asc or :desc

  • :indexset (NSIndexSet)

Yields:

  • (asset, error)

Yield Parameters:



109
110
111
# File 'lib/motional/asset.rb', line 109

def self.find_all(options = {}, &block)
  self.origin_find_all(options, block)
end

.find_by_url(asset_url) {|asset, error| ... } ⇒ nil

Find an asset by a specified asset_url.

Examples:

MotionAL::Asset.find_by_url(url) do |asset, error|
  # asynchronous if a block given
  p asset.url.absoluteString
end

Yields:

  • (asset, error)

Yield Parameters:



75
76
77
78
# File 'lib/motional/asset.rb', line 75

def self.find_by_url(asset_url, &block)
  url = asset_url.is_a?(String) ? NSURL.alloc.initWithString(asset_url) : asset_url
  self.origin_find_by_url(url, block)
end

.video_compatible?(video_path_url) ⇒ Boolean



117
118
119
# File 'lib/motional/asset.rb', line 117

def self.video_compatible?(video_path_url)
  MotionAL.library.al_asset_library.videoAtPathIsCompatibleWithSavedPhotosAlbum(video_path_url)
end

Instance Method Details

#aspect_ratio_thumbnailCGImageRef



128
129
130
# File 'lib/motional/asset.rb', line 128

def aspect_ratio_thumbnail
  self.al_asset.aspectRatioThumbnail
end

#asset_typeSymbol

The type of the asset.



186
187
188
# File 'lib/motional/asset.rb', line 186

def asset_type
  MotionAL.asset_types.key(@al_asset.valueForProperty(ALAssetPropertyType))
end

#cg_imageCGImageRef?

The default representation’s cg_image



215
# File 'lib/motional/asset.rb', line 215

make_wrapper_for_representation_method(:cg_image, "CGImageRef")

#dataNSConcreteData?

The default representation’s data



214
# File 'lib/motional/asset.rb', line 214

make_wrapper_for_representation_method(:data, "NSConcreteData")

#dateTime?

The asset’s date



175
# File 'lib/motional/asset.rb', line 175

make_wrapper_for_property(:date, ALAssetPropertyDate, "Time")

#default_representationMotionAL::Representation Also known as: rep, file, representation



152
153
154
# File 'lib/motional/asset.rb', line 152

def default_representation
  @default_representation ||= Representation.new(@asset, @al_asset.defaultRepresentation)
end

#dimensionsCGSize?

The default representation’s dimensions



216
# File 'lib/motional/asset.rb', line 216

make_wrapper_for_representation_method(:dimensions, "CGSize")

#durationFloat?

The asset’s duration



174
# File 'lib/motional/asset.rb', line 174

make_wrapper_for_property(:duration, ALAssetPropertyDuration, "Float")

#editable?Boolean

Return true if the app haves write access for the asset. In other words true means the app can call ‘#update` for the asset.



136
137
138
# File 'lib/motional/asset.rb', line 136

def editable?
  @al_asset.editable?
end

#filenameString?

The default representation’s filename



217
# File 'lib/motional/asset.rb', line 217

make_wrapper_for_representation_method(:filename, "String")

#full_resolution_imageCGImageRef?

The default representation’s full_resolution_image



211
# File 'lib/motional/asset.rb', line 211

make_wrapper_for_representation_method(:full_resolution_image, "CGImageRef")

#full_screen_imageCGImageRef?

The default representation’s full_screen_image



212
# File 'lib/motional/asset.rb', line 212

make_wrapper_for_representation_method(:full_screen_image, "CGImageRef")

#locationCLLocation?

The asset’s location



173
# File 'lib/motional/asset.rb', line 173

make_wrapper_for_property(:location, ALAssetPropertyLocation, "CLLocation")

#metadataHash?

The default representation’s metadata



219
# File 'lib/motional/asset.rb', line 219

make_wrapper_for_representation_method(:metadata, "Hash")

#orientationSymbol

The orientation of the asset.



193
194
195
# File 'lib/motional/asset.rb', line 193

def orientation
  MotionAL.asset_orientations.key(@al_asset.valueForProperty(ALAssetPropertyOrientation))
end

#original_assetMotionAL::Asset?

Note:

The original asset was set when the asset was created by ‘#save_new`

The original version of the asset.



146
147
148
149
# File 'lib/motional/asset.rb', line 146

def original_asset
  original_al_asset = @al_asset.originalAsset
  Asset.new(original_al_asset) if original_al_asset
end

#representation_urlsArray?

The asset’s representation_urls



178
# File 'lib/motional/asset.rb', line 178

make_wrapper_for_property(:representation_urls, ALAssetPropertyURLs, "Array")

#representation_utisArray?

The asset’s representation_utis



177
# File 'lib/motional/asset.rb', line 177

make_wrapper_for_property(:representation_utis, ALAssetPropertyRepresentations, "Array")

#representationsMotionAL::Representations Also known as: reps, files



22
23
24
# File 'lib/motional/asset.rb', line 22

def representations
  @representations ||= Representations.new(self)
end

#save_new(source, metadata = nil) {|asset, error| ... } ⇒ nil

Note:

use ‘MotionAL::Asset.video_compatible?(video_path_url)` before creating video.

Create a new asset forked by the asset.

Examples:

asset_a.save_new(imagedata, meta) do |asset, error| do
  # asynchronous if a block given
  p asset.url.absoluteString
  p asset.original_asset.url.absoluteString 
end

asset_a.create(imagedata, meta)

Yields:

  • (asset, error)

Yield Parameters:



241
242
243
# File 'lib/motional/asset.rb', line 241

def save_new(source,  = nil, &block)
  origin_save_new(source, , block)
end

#scaleFloat?

The default representation’s scale



213
# File 'lib/motional/asset.rb', line 213

make_wrapper_for_representation_method(:scale, "Float")

#sizeFixnum?

The default representation’s size



218
# File 'lib/motional/asset.rb', line 218

make_wrapper_for_representation_method(:size, "Fixnum")

#thumbnailCGImageRef



123
124
125
# File 'lib/motional/asset.rb', line 123

def thumbnail
  self.al_asset.thumbnail
end

#update(source, metadata = nil) {|asset, error| ... } ⇒ nil

Note:

use ‘MotionAL::Asset.video_compatible?(video_path_url)` before updating video.

Update the asset. In other words replacing the asset’s representation.

Examples:

asset_a.update(imagedata, meta) do |asset, error| do
  # asynchronous if a block given
  p asset.url.absoluteString
end

asset_a.update(imagedata, meta)

Yields:

  • (asset, error)

Yield Parameters:



265
266
267
# File 'lib/motional/asset.rb', line 265

def update(source,  = nil, &block)
  origin_update(source, , block)
end

#urlNSURL?

The asset’s url



176
# File 'lib/motional/asset.rb', line 176

make_wrapper_for_property(:url, ALAssetPropertyAssetURL, "NSURL")