PhotoCollection

public class PhotoCollection : Sequence

A PhotoCollection represents a group of Photos that are all the same size. Collections are stored in a folder in the app bundle and loaded by the static methods provided in this class. A PhotoCollection is a sequence. Thus you can iterator over collections. Example:

// get names of all folders in "Photo Collections"
let collectionNames = PhotoCollection.namesOfCollections(inFolder: "Photo Collections")

// create collection for "swatches" subfolder and iterate over the photos
// in that collection
let collection = PhotoCollection.collection(named: "swatches", inFolder: "Photo Collections")
for photo in collection {
  ...
}

Specification Properties:

  • name : String - The name of the collection
  • self : Sequence - The photos in the collecton
  • photoSize : Size - the size of each photo

Abstract Invariant:

  • all photos stored in the collection has size photoSize.
  • Name of the collection

    Declaration

    Swift

    public let name: String
  • size of each photo in collection

    Declaration

    Swift

    public let photoSize: Size
  • Create a new PhotoCollection with the given name and photos.

    Requres: photos.count > 0

    Effects: Createa a new object w/ the givven properties.

    Declaration

    Swift

    public init(name: String, photos: [Photo])

    Parameters

    name

    Name of the collection.

    photos

    The photos in the collection.

  • Return an iterator for the collections

    Declaration

    Swift

    public func makeIterator() -> IndexingIterator<[Photo]>
  • Find a photo with the given name in the collection.

    Declaration

    Swift

    public func photo(named name: String) -> Photo?

    Parameters

    name

    The photo name to look for.

    Return Value

    A photo with that name or nil if no such photo exists.

  • An image showing all of the photos in the collection, arranged in a grid. This is useful to verify that collections are loading correctly and to see what images the contains.

    Note

    This may be computation expensive and should not run on the main UI thread.

    Declaration

    Swift

    public var image: UIImage { get }
  • Return a list of collection names, based on the folders found on the given path.

    The path is relative to the root of the resources for the main bundle of the app. Use the optional bundle parameter to specify looking in the resources for a different bundle.

    Examples:

    1. To get the collections in the main bundles Photo Collections folder, include the following in any method also belonging to the main bundle:
    if let names = PhotoCollection.namesOfCollections(inFolder: "Photo Collections") {
    ...
    }
    
    1. To get the collections in the Photo Collections folder stored in the bundle for the PhotoMosaicTests.swift unit tests, include the following in a method of the PhotoMosaicTests class.
    if let names = PhotoCollection.namesOfCollections(inFolder: "Photo Collections",
    inBundle: Bundle(for: type(of: self))) {
    ...
    }
    

    Declaration

    Swift

    static public func namesOfCollections(inFolder path: String, inBundle bundle : Bundle = Bundle.main) -> [String]?

    Parameters

    path

    folder relative to the root of the resouces folder for bundle in which to look for collections.

    bundle

    The bundle in which to look. Default: Bundle.main

    Return Value

    An array of collection names, or nil if the path/bundle are invalid.

  • Create and return a collection with the given name at the given path.

    The path is relative to the root of the resources for the main bundle of the app. Use the optional bundle parameter to specify looking in the resources for a different bundle.

    Examples:

    1. To get the collection of photos in the directory Photo Collections/swatches in the main bundles, include the following in any method also belonging to the main bundle:
    if let collection = PhotoCollection.collection(named: "swatches",
    inFolder: "Photo Collections") {
    ...
    }
    
    1. To get the collection of photos in the directory Photo Collections/swatches-small in the bundle for the PhotoMosaicTests.swift unit tests, include the following in a method of the PhotoMosaicTests class:
    if let collection = PhotoCollection.collection(named: "swatches",
    inFolder: "Photo Collections",
    inBundle: Bundle(for: type(of: self))) {
    ...
    }
    

    Declaration

    Swift

    static public func collection(named name: String, inFolder path: String, inBundle bundle : Bundle = Bundle.main) -> PhotoCollection?

    Parameters

    name

    Name of the folder inside the folder at path to use as the collection.

    path

    folder relative to the root of the resouces folder for bundle in which to look for collections.

    bundle

    The bundle in which to look. Default: Bundle.main

    Return Value

    A PhotoCollection of photos stored in the folder path/name inside the given bundle, or nil if not such path/name exists in the bundle