MediaProxyService

public class MediaProxyService : NSObject
extension MediaProxyService: URLSessionDownloadDelegate

A service class for interacting with the TC infrastructure to proxy NFT images, videos and audio files

  • Enum denoting the avaialble sizes for media, in a human friendly, scale agnostic manner

    See more

    Declaration

    Swift

    public enum Format : String, Codable
  • Enum denoting the avaialble sizes for media in the specific values available on the server

    See more

    Declaration

    Swift

    public enum RawFormat : String, Codable
  • Supported source types for proxied media

    See more

    Declaration

    Swift

    public enum Source : String, Codable
  • Supported media types

    See more

    Declaration

    Swift

    public enum MediaType : String, Codable
  • Helper to parse a collection of media types to understand its contents

    See more

    Declaration

    Swift

    public enum AggregatedMediaType : String, Codable
  • Declaration

    Swift

    public static var isDarkMode: Bool
  • Declaration

    Swift

    public static func setupImageLibrary()

URL conversion

  • Take a URI from a token metadata response and convert it to a useable media proxy URL

    Declaration

    Swift

    public static func url(fromUriString uri: String?, ofFormat format: RawFormat, keepGif: Bool = false) -> URL?

    Parameters

    fromUriString

    String containing a URI (supports IPFS URIs)

    ofFormat

    The requested format from the proxy

    Return Value

    An optional URL

  • Take a URI from a token metadata response and convert it to a useable media proxy URL

    Declaration

    Swift

    public static func url(fromUri uri: URL?, ofFormat format: RawFormat) -> URL?

    Parameters

    fromUri

    URL object containing a URI (supports IPFS URIs)

    ofFormat

    The requested format from the proxy

    Return Value

    An optional URL

  • Helper method to return a standard thumbnail URL for a NFT, taking into account some custom logic / known workarounds

    Declaration

    Swift

    public static func iconURL(forNFT nft: NFT) -> URL?

    Parameters

    fromNFT

    NFT object

    Return Value

    An optional URL

  • Helper method to return a standard small version of the display URL for a NFT

    Declaration

    Swift

    public static func smallURL(forNFT nft: NFT) -> URL?

    Parameters

    fromNFT

    NFT object

    Return Value

    An optional URL

  • Helper method to return a standard medium version of the display URL for a NFT

    Declaration

    Swift

    public static func mediumURL(forNFT nft: NFT) -> URL?

    Parameters

    fromNFT

    NFT object

    Return Value

    An optional URL

  • Helper method to return a standard large version of the display URL for a NFT

    Declaration

    Swift

    public static func largeURL(forNFT nft: NFT) -> URL?

    Parameters

    fromNFT

    NFT object

    Return Value

    An optional URL

Type checking

  • Using only info from TzKTBalanceMetadataFormat determine the media type(s) of the object

    Declaration

    Swift

    public static func getMediaType(fromFormats formats: [TzKTBalanceMetadataFormat]) -> [MediaType]
  • Given multiple sources of information, attempt to find the media type the url is pointing too

    Declaration

    Swift

    public func getMediaType(fromFormats formats: [TzKTBalanceMetadataFormat], orURL url: URL?, urlSession: URLSession = .shared, completion: @escaping ((Result<[MediaType], KukaiError>) -> Void))

    Parameters

    fromFormats

    An array of TzKTBalanceMetadataFormat that comes down with the TzKTClient’s balancing fetching code. It MAY contain infomration on the media type

    orURL

    The URL for the record. It MAY contain a file extension dennoting the file type

    urlSession

    If type can’t be found via URL or metadata, download the first packet, examine the headers for Content-Type using this session. (HEAD requests aren’t currently supported if the asset hasn’t been already cached)

    completion

    A block to run when a type can be found, or an error encountered

  • Helper method to parse an array of MediaType to quickly determine its content type so UI can be easily arraged

    Declaration

    Swift

    public static func typesContents(_ types: [MediaType]) -> AggregatedMediaType?

Cache management

Image loading

  • Attempt to use KingFisher library to load an image from a URL, into an UIImageView, with support for downsampling, displaying loading spinner, svgs, gifs and the permanent / temporary caching system

    Declaration

    Swift

    public static func load(url: URL?, to imageView: UIImageView, withCacheType cacheType: CacheType, fallback: UIImage, downSampleSize: CGSize? = nil, maxAnimatedImageSize: UInt? = nil, completion: ((CGSize?) -> Void)? = nil)

    Parameters

    url

    Media proxy URL pointing to an image

    to

    The UIImageView to load the image into

    fromCache

    Which cahce to search for the image, or load it into if not found and needs to be downloaded

    fallback

    If an error occurs and an image can’t be downloaded/loaded in, display this image instead

    downSampleSize

    Supply the dimensions you wish the image to be resized to fit

    maxAnimatedImageSize

    set a size limit for animated images (in bytes). If exceeded, will only load the first frame of the image

    completion

    returns when operation finished, if successful it will return the downloaded image’s CGSize

  • Declaration

    Swift

    public static func imageCache(forType: CacheType) -> SDImageCache
  • Attempt to use KingFisher library to load an image from a URL, and store it directly in the cache for later usage. Also optional return the downloaded size via a completion block, useful for preparing table/collection view

    Declaration

    Swift

    public static func cacheImage(url: URL?, cacheType: CacheType = .temporary, completion: @escaping ((CGSize?) -> Void))

    Parameters

    url

    Media proxy URL pointing to an image

    fromCache

    Which cahce to search for the image, or load it into if not found and needs to be downloaded

    completion

    returns when operation finished, if successful it will return the downloaded image’s CGSize

  • Check if a given url is already cached

    Declaration

    Swift

    public static func isCached(url: URL?, cacheType: CacheType = .temporary) -> Bool
  • Check if an image is cached, and return its size if so. Useful for preparing table/collection view

    Declaration

    Swift

    public static func sizeForImageIfCached(url: URL?, cacheType: CacheType = .temporary) -> CGSize?

    Parameters

    url

    Media proxy URL pointing to an image

    fromCache

    Which cahce to search for the image, or load it into if not found and needs to be downloaded

  • Declaration

    Swift

    public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL)
  • Declaration

    Swift

    public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
  • Declaration

    Swift

    public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)