Asset

public class Asset : CachePolicyAccessible
extension Asset: ResourceQueryable

Asset refer to all the media files (images, videos, PDFs, audio files, and so on) uploaded in your Contentstack repository for future use. Learn more about Asset.

  • Declaration

    Swift

    public var cachePolicy: CachePolicy
  • Instance method to fetch Asset for specific locale.

    Example usage:

    let stack = Contentstack.stack(apiKey: apiKey,
                deliveryToken: deliveryToken,
                environment: environment)
    
    // To retrive single asset with specific locale
    stack.asset(uid: assetUID).locale("en-us")
    .fetch { (result: Result<AssetModel, Error>, response: ResponseType) in
       switch result {
       case .success(let model):
             //Model retrive from API
       case .failure(let error):
             //Error Message
       }
    }
    

    Declaration

    Swift

    public func locale(_ locale: String) -> Self

    Parameters

    locale

    The code for fetching entry for locale.

    Return Value

    A Asset to enable chaining.

  • To include the relative URLs of the assets in the response.

    Example usage:

    let stack = Contentstack.stack(apiKey: apiKey,
                deliveryToken: deliveryToken,
                environment: environment)
    
    // To retrive single asset with relative URL
    let asset = stack.asset(uid: assetUID).includeRelativeURL()
    .fetch { (result: Result<AssetModel, Error>, response: ResponseType) in
       switch result {
       case .success(let model):
             //Model retrive from API
       case .failure(let error):
             //Error Message
       }
    }
    

    Declaration

    Swift

    public func includeRelativeURL() -> Asset

    Return Value

    A Asset to enable chaining.

  • To include the fallback published content if specified locale content is not publish.

    Example usage:

    let stack = Contentstack.stack(apiKey: apiKey,
                deliveryToken: deliveryToken,
                environment: environment)
    
    // To retrive single asset with relative URL
    let asset = stack.asset(uid: assetUID).includeFallback()
    .fetch { (result: Result<AssetModel, Error>, response: ResponseType) in
       switch result {
       case .success(let model):
             //Model retrive from API
       case .failure(let error):
             //Error Message
       }
    }
    

    Declaration

    Swift

    public func includeFallback() -> Asset

    Return Value

    A Asset to enable chaining.

  • To include the dimensions (height and width) of the image in the response.

    Precondition

    Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, SVG, and PSD.

    Example usage:

    let stack = Contentstack.stack(apiKey: apiKey,
                deliveryToken: deliveryToken,
                environment: environment)
    
    // To retrive single asset with dimension
    stack.asset(uid: assetUID).includeDimension()
    .fetch { (result: Result<AssetModel, Error>, response: ResponseType) in
       switch result {
       case .success(let model):
            //Model retrive from API
       case .failure(let error):
            //Error Message
       }
    }
    

    Declaration

    Swift

    public func includeDimension() -> Asset

    Return Value

    A Asset to enable chaining.

  • To fetch all or find Assets query method is used.

    Example usage:

    let stack = Contentstack.stack(apiKey: apiKey,
                deliveryToken: deliveryToken,
                environment: environment)
    
    stack.asset().query()
    .find { (result: Result<ContentstackResponse<AssetModel>, Error>, response: ResponseType) in
        switch result {
        case .success(let contentstackResponse):
            // Contentstack response with AssetModel array in items.
        case .failure(let error):
            //Error Message
        }
    }
    

    Declaration

    Swift

    public func query() -> AssetQuery

    Return Value

    A AssetQuery to enable chaining.

  • This call fetches the latest version of a specific Asset of a particular stack.

    Example usage:

    let stack = Contentstack.stack(apiKey: apiKey,
                deliveryToken: deliveryToken,
                environment: environment)
    
    stack.asset(uid: assetUID)
    .fetch { (result: Result<AssetModel, Error>, response: ResponseType) in
       switch result {
       case .success(let model):
            //Model retrive from API
       case .failure(let error):
            //Error Message
       }
    }
    

    Declaration

    Swift

    public func fetch<ResourceType>(_ completion: @escaping (Result<ResourceType, Error>, ResponseType) -> Void)
        where ResourceType: EndpointAccessible, ResourceType: Decodable

    Parameters

    completion

    A handler which will be called on completion of the operation.