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 moreDeclaration
Swift
public enum Format : String, Codable
-
Enum denoting the avaialble sizes for media in the specific values available on the server
See moreDeclaration
Swift
public enum RawFormat : String, Codable
-
Supported source types for proxied media
See moreDeclaration
Swift
public enum Source : String, Codable
-
Supported media types
See moreDeclaration
Swift
public enum MediaType : String, Codable
-
Helper to parse a collection of media types to understand its contents
See moreDeclaration
Swift
public enum AggregatedMediaType : String, Codable
-
Declaration
Swift
public static var isDarkMode: Bool
-
Declaration
Swift
public static func setupImageLibrary()
-
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
-
Using only info from
TzKTBalanceMetadataFormat
determine the media type(s) of the objectDeclaration
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 typeorURL
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 arragedDeclaration
Swift
public static func typesContents(_ types: [MediaType]) -> AggregatedMediaType?
-
Clear all images from all caches
Declaration
Swift
public static func removeAllImages(completion: @escaping (() -> Void))
-
Declaration
Swift
public static func removeAllImages(fromCache: CacheType, completion: @escaping (() -> Void))
-
Clear only iamges from cahce that have expired
Declaration
Swift
public static func clearExpiredImages()
-
Get size in bytes
Declaration
Swift
public static func sizeOf(cache: CacheType) -> UInt
-
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
Parameters
url
Media proxy URL pointing to an image
to
The
UIImageView
to load the image intofromCache
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)