Class: MotionAL::Group

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

Overview

A wrapper of ALAssetGroup class.

An ALAssetsGroup object represents an ordered set of the assets managed by the Photos application. The order of the elements is the same as the user sees in the Photos application. An asset can belong to multiple assets groups.
Assets groups themselves are synced via iTunes, created to hold the user’s saved photos or created during camera import. You cannot directly modify the groups using ALAssetsGroup. You can indirectly modify the Saved Photos group by saving images or videos into it using the ALAssetsLibrary class.

And added some convinience methods.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(al_asset_group) ⇒ Group

Returns a new instance of Group.

Parameters:

  • al_asset_group (ALAssetsGroup)


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

def initialize(al_asset_group)
  @al_asset_group = al_asset_group
end

Instance Attribute Details

#al_asset_groupObject (readonly)

An instance of ALAssetGroup.



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

def al_asset_group
  @al_asset_group
end

Class Method Details

.create(group_name) {|group, error| ... } ⇒ nil

Create a group. A group should not be created if a specified name already exists.

Examples:

MotionAL::Group.create('MyAlbum') do |group, error|
  # asynchronous if a block given
  p group.name
end

MotionAL::Group.create('MyAlbum')

Parameters:

  • group_name (String)

Yields:

  • (group, error)

Yield Parameters:

Returns:

  • (nil)


38
39
40
# File 'lib/motional/group.rb', line 38

def self.create(group_name, &block)
  self.origin_create(group_name, block)
end

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

Note:

group_type :all includes all groups except ‘Photo Library’

Find and enumerate all groups in the AssetLibrary.

Examples:

MotionAL::group.find_all do |group, error|
  # asynchronous
  p group.name
end

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :group_type (Symbol)

    An asset group type. default: :all.

Yields:

  • (group, error)

Yield Parameters:

Returns:

  • (nil)

See Also:



133
134
135
# File 'lib/motional/group.rb', line 133

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

.find_by_name(group_name) {|group, error| ... } ⇒ nil

Note:

It is recommended to use find_by_url instead of this. Because a group could be renamed.

Find a group by a specified group name.

Examples:

MotionAL::Group.find_by_name('MyAlbum') do |group, error|
  p group.name
end

Parameters:

  • group_name (String)

Yields:

  • (group, error)

Yield Parameters:

Returns:

  • (nil)


76
77
78
79
80
81
# File 'lib/motional/group.rb', line 76

def self.find_by_name(group_name, &block)
  group_name = /^#{group_name}$/ if group_name.kind_of? String
  find_all do |group, error|
    block.call(group, error) if group.name =~ group_name
  end
end

.find_by_url(group_url) {|group, error| ... } ⇒ nil

Find a group by a specified group_url.

Examples:

MotionAL::group.find_by_url(url) do |group, error|
  # asynchronous
  p group.name
end

Parameters:

  • group_url (NSURL, String)

Yields:

  • (group, error)

Yield Parameters:

Returns:

  • (nil)


56
57
58
59
# File 'lib/motional/group.rb', line 56

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

.find_camera_roll {|group, error| ... } ⇒ nil

Find the Camera Roll(built-in default group)

Examples:

MotionAL::Group.find_camera_roll do |group, error|
  p group.name #=> 'Camera Roll' or 'Saved Photos'
end

Yields:

  • (group, error)

Yield Parameters:

  • group (MotionAL::Group)

    ‘Camera Roll’ or ‘Saved Photos’

  • error (error)

Returns:

  • (nil)


95
96
97
# File 'lib/motional/group.rb', line 95

def self.find_camera_roll(&block)
  find_by_name(/Camera Roll|Saved Photos/) {|group, error| block.call(group, error) }
end

.find_photo_library {|group, error| ... } ⇒ nil

Find the Photo Library(synced from iTunes)

Examples:

MotionAL::Group.find_photo_library do |group, error|
  p group.name #=> 'Photo Library'
end

Yields:

  • (group, error)

Yield Parameters:

Returns:

  • (nil)


111
112
113
# File 'lib/motional/group.rb', line 111

def self.find_photo_library(&block)
  find_all({group_type: :library}) { |group, error| block.call(group, error) }
end

Instance Method Details

#add_asset(asset) ⇒ Boolean

Note:

cannot remove ALAsset from ALAssetGroup by yor app

Add an asset to the group.

Parameters:

Returns:

  • (Boolean)

    true if asset was added successfully, otherwise false



165
166
167
# File 'lib/motional/group.rb', line 165

def add_asset(asset)
  @al_asset_group.addAsset(asset.al_asset)
end

#asset_group_typeSymbol

The type of the group.

Returns:

  • (Symbol)

    :library, :album, :event, :faces, :photos, :photo_stream or :all



192
193
194
# File 'lib/motional/group.rb', line 192

def asset_group_type
  MotionAL.asset_group_types.key(@al_asset_group.valueForProperty(ALAssetsGroupPropertyType))
end

#assetsMotionAL::Assets

The collection of assets in the group.

Returns:



142
143
144
# File 'lib/motional/group.rb', line 142

def assets
  @assets ||= Assets.new(self)
end

#editable?Boolean

Return true if your app has write access for the group. In other words true means your app can add assets to the group.

Returns:

  • (Boolean)


150
151
152
# File 'lib/motional/group.rb', line 150

def editable?
  @al_asset_group.editable?
end

#nameString?

The gruop’s name

Returns:

  • (String)

    The value for the property ALAssetsGroupPropertyName.

  • (nil)

    The property is empty.



185
# File 'lib/motional/group.rb', line 185

make_wrapper_for_property(:name, ALAssetsGroupPropertyName, "String")

#persistent_idString?

The gruop’s persistent_id

Returns:

  • (String)

    The value for the property ALAssetsGroupPropertyPersistentID.

  • (nil)

    The property is empty.



186
# File 'lib/motional/group.rb', line 186

make_wrapper_for_property(:persistent_id, ALAssetsGroupPropertyPersistentID, "String")

#poster_imageCGImageRef

Returns The group’s poster image.

Returns:

  • (CGImageRef)

    The group’s poster image.



155
156
157
# File 'lib/motional/group.rb', line 155

def poster_image
  @al_asset_group.posterImage
end

#urlNSURL?

The gruop’s url

Returns:

  • (NSURL)

    The value for the property ALAssetsGroupPropertyURL.

  • (nil)

    The property is empty.



187
# File 'lib/motional/group.rb', line 187

make_wrapper_for_property(:url, ALAssetsGroupPropertyURL, "NSURL")