AlamofireExtension
extension AlamofireExtension where ExtendedType: UIButton
extension AlamofireExtension where ExtendedType: UIImage
extension AlamofireExtension where ExtendedType: UIImageView
-
The instance image downloader used to download all images. If this property is
nil, theUIButtonwill fallback on thesharedImageDownloaderfor all downloads. The most common use case for needing to use a custom instance image downloader is when images are behind different basic auth credentials.Declaration
Swift
public var imageDownloader: ImageDownloader? { get nonmutating set } -
The shared image downloader used to download all images. By default, this is the default
ImageDownloaderinstance backed with anAutoPurgingImageCachewhich automatically evicts images from the cache when the memory capacity is reached or memory warning notifications occur. The shared image downloader is only used if theimageDownloaderisnil.Declaration
Swift
public static var sharedImageDownloader: ImageDownloader { get set }
-
Asynchronously downloads an image from the specified URL and sets it once the request is finished.
If the image is cached locally, the image is set immediately. Otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the image request is finished.
Declaration
Swift
public func setImage(for state: ControlState, url: URL, cacheKey: String? = nil, placeholderImage: UIImage? = nil, serializer: ImageResponseSerializer? = nil, filter: ImageFilter? = nil, progress: ImageDownloader.ProgressHandler? = nil, progressQueue: DispatchQueue = DispatchQueue.main, completion: ((AFIDataResponse<UIImage>) -> Void)? = nil)Parameters
stateThe control state of the button to set the image on.
urlThe URL used for your image request.
cacheKeyAn optional key used to identify the image in the cache. Defaults to
nil.placeholderImageThe image to be set initially until the image request finished. If
nil, the image will not change its image until the image request finishes. Defaults tonil.serializerImage response serializer used to convert the image data to
UIImage. Defaults tonilwhich will fall back to the instanceimageResponseSerializerset on theImageDownloader.filterThe image filter applied to the image after the image request is finished. Defaults to
nil.progressThe closure to be executed periodically during the lifecycle of the request. Defaults to
nil.progressQueueThe dispatch queue to call the progress closure on. Defaults to the main queue.
completionA closure to be executed when the image request finishes. The closure takes a single response value containing either the image or the error that occurred. If the image was returned from the image cache, the response will be
nil. Defaults tonil. -
setImage(for:urlRequest: cacheKey: placeholderImage: serializer: filter: progress: progressQueue: completion: ) Asynchronously downloads an image from the specified URL and sets it once the request is finished.
If the image is cached locally, the image is set immediately. Otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the image request is finished.
Declaration
Swift
public func setImage(for state: ControlState, urlRequest: URLRequestConvertible, cacheKey: String? = nil, placeholderImage: UIImage? = nil, serializer: ImageResponseSerializer? = nil, filter: ImageFilter? = nil, progress: ImageDownloader.ProgressHandler? = nil, progressQueue: DispatchQueue = DispatchQueue.main, completion: ((AFIDataResponse<UIImage>) -> Void)? = nil)Parameters
stateThe control state of the button to set the image on.
urlRequestThe URL request.
cacheKeyAn optional key used to identify the image in the cache. Defaults to
nil.placeholderImageThe image to be set initially until the image request finished. If
nil, the image will not change its image until the image request finishes. Defaults tonil.serializerImage response serializer used to convert the image data to
UIImage. Defaults tonilwhich will fall back to the instanceimageResponseSerializerset on theImageDownloader.filterThe image filter applied to the image after the image request is finished. Defaults to
nil.progressThe closure to be executed periodically during the lifecycle of the request. Defaults to
nil.progressQueueThe dispatch queue to call the progress closure on. Defaults to the main queue.
completionA closure to be executed when the image request finishes. The closure takes a single response value containing either the image or the error that occurred. If the image was returned from the image cache, the response will be
nil. Defaults tonil. -
Cancels the active download request for the image, if one exists.
Declaration
Swift
public func cancelImageRequest(for state: ControlState)
-
setBackgroundImage(for:url: cacheKey: placeholderImage: serializer: filter: progress: progressQueue: completion: ) Asynchronously downloads an image from the specified URL and sets it once the request is finished.
If the image is cached locally, the image is set immediately. Otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the image request is finished.
Declaration
Swift
public func setBackgroundImage(for state: ControlState, url: URL, cacheKey: String? = nil, placeholderImage: UIImage? = nil, serializer: ImageResponseSerializer? = nil, filter: ImageFilter? = nil, progress: ImageDownloader.ProgressHandler? = nil, progressQueue: DispatchQueue = DispatchQueue.main, completion: ((AFIDataResponse<UIImage>) -> Void)? = nil)Parameters
stateThe control state of the button to set the image on.
urlThe URL used for the image request.
cacheKeyAn optional key used to identify the image in the cache. Defaults to
nil.placeholderImageThe image to be set initially until the image request finished. If
nil, the background image will not change its image until the image request finishes. Defaults tonil.serializerImage response serializer used to convert the image data to
UIImage. Defaults tonilwhich will fall back to the instanceimageResponseSerializerset on theImageDownloader.filterThe image filter applied to the image after the image request is finished. Defaults to
nil.progressThe closure to be executed periodically during the lifecycle of the request. Defaults to
nil.progressQueueThe dispatch queue to call the progress closure on. Defaults to the main queue.
completionA closure to be executed when the image request finishes. The closure takes a single response value containing either the image or the error that occurred. If the image was returned from the image cache, the response will be
nil. Defaults tonil. -
setBackgroundImage(for:urlRequest: cacheKey: placeholderImage: serializer: filter: progress: progressQueue: completion: ) Asynchronously downloads an image from the specified URL request and sets it once the request is finished.
If the image is cached locally, the image is set immediately. Otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the image request is finished.
Declaration
Swift
public func setBackgroundImage(for state: ControlState, urlRequest: URLRequestConvertible, cacheKey: String? = nil, placeholderImage: UIImage? = nil, serializer: ImageResponseSerializer? = nil, filter: ImageFilter? = nil, progress: ImageDownloader.ProgressHandler? = nil, progressQueue: DispatchQueue = DispatchQueue.main, completion: ((AFIDataResponse<UIImage>) -> Void)? = nil)Parameters
stateThe control state of the button to set the image on.
urlRequestThe URL request.
cacheKeyAn optional key used to identify the image in the cache. Defaults to
nil.placeholderImageThe image to be set initially until the image request finished. If
nil, the background image will not change its image until the image request finishes. Defaults tonil.serializerImage response serializer used to convert the image data to
UIImage. Defaults tonilwhich will fall back to the instanceimageResponseSerializerset on theImageDownloader.filterThe image filter applied to the image after the image request is finished. Defaults to
nil.progressThe closure to be executed periodically during the lifecycle of the request. Defaults to
nil.progressQueueThe dispatch queue to call the progress closure on. Defaults to the main queue.
completionA closure to be executed when the image request finishes. The closure takes a single response value containing either the image or the error that occurred. If the image was returned from the image cache, the response will be
nil. Defaults tonil. -
Cancels the active download request for the background image, if one exists.
Declaration
Swift
public func cancelBackgroundImageRequest(for state: ControlState)
-
Initializes and returns the image object with the specified data in a thread-safe manner.
It has been reported that there are thread-safety issues when initializing large amounts of images simultaneously. In the event of these issues occurring, this method can be used in place of the
init?(data:)method.Declaration
Swift
public static func threadSafeImage(with data: Data) -> UIImage?Parameters
dataThe data object containing the image data.
Return Value
An initialized
UIImageobject, ornilif the method failed. -
Initializes and returns the image object with the specified data and scale in a thread-safe manner.
It has been reported that there are thread-safety issues when initializing large amounts of images simultaneously. In the event of these issues occurring, this method can be used in place of the
init?(data:scale:)method.Declaration
Swift
public static func threadSafeImage(with data: Data, scale: CGFloat) -> UIImage?Parameters
dataThe data object containing the image data.
scaleThe scale factor to assume when interpreting the image data. Applying a scale factor of 1.0 results in an image whose size matches the pixel-based dimensions of the image. Applying a different scale factor changes the size of the image as reported by the size property.
Return Value
An initialized
UIImageobject, ornilif the method failed. -
Returns whether the image is inflated.
Declaration
Swift
public var isInflated: Bool { get nonmutating set } -
Inflates the underlying compressed image data to be backed by an uncompressed bitmap representation.
Inflating compressed image formats (such as PNG or JPEG) can significantly improve drawing performance as it allows a bitmap representation to be constructed in the background rather than on the main thread.
Declaration
Swift
public func inflate() -
Returns whether the image contains an alpha component.
Declaration
Swift
public var containsAlphaComponent: Bool { get } -
Returns whether the image is opaque.
Declaration
Swift
public var isOpaque: Bool { get } -
Returns a new version of the image scaled to the specified size.
Declaration
Swift
public func imageScaled(to size: CGSize, scale: CGFloat? = nil) -> UIImageParameters
sizeThe size to use when scaling the new image.
scaleThe scale to set for the new image. Defaults to
nilwhich will maintain the current image scale.Return Value
The new image object.
-
Returns a new version of the image scaled from the center while maintaining the aspect ratio to fit within a specified size.
The resulting image contains an alpha component used to pad the width or height with the necessary transparent pixels to fit the specified size. In high performance critical situations, this may not be the optimal approach. To maintain an opaque image, you could compute the
scaledSizemanually, then use theaf.imageScaledToSizemethod in conjunction with a.Centercontent mode to achieve the same visual result.Declaration
Swift
public func imageAspectScaled(toFit size: CGSize, scale: CGFloat? = nil) -> UIImageParameters
sizeThe size to use when scaling the new image.
scaleThe scale to set for the new image. Defaults to
nilwhich will maintain the current image scale.Return Value
A new image object.
-
Returns a new version of the image scaled from the center while maintaining the aspect ratio to fill a specified size. Any pixels that fall outside the specified size are clipped.
Declaration
Swift
public func imageAspectScaled(toFill size: CGSize, scale: CGFloat? = nil) -> UIImageParameters
sizeThe size to use when scaling the new image.
scaleThe scale to set for the new image. Defaults to
nilwhich will maintain the current image scale.Return Value
A new image object.
-
Returns a new version of the image with the corners rounded to the specified radius.
Declaration
Swift
public func imageRounded(withCornerRadius radius: CGFloat, divideRadiusByImageScale: Bool = false) -> UIImageParameters
radiusThe radius to use when rounding the new image.
divideRadiusByImageScaleWhether to divide the radius by the image scale. Set to
truewhen the image has the same resolution for all screen scales such as @1x, @2x and @3x (i.e. single image from web server). Set tofalsefor images loaded from an asset catalog with varying resolutions for each screen scale.falseby default.Return Value
A new image object.
-
Returns a new version of the image rounded into a circle.
Declaration
Swift
public func imageRoundedIntoCircle() -> UIImageReturn Value
A new image object.
-
Returns a new version of the image using a CoreImage filter with the specified name and parameters.
Declaration
Swift
public func imageFiltered(withCoreImageFilter name: String, parameters: [String : Any]? = nil) -> UIImage?Parameters
nameThe name of the CoreImage filter to use on the new image.
parametersThe parameters to apply to the CoreImage filter.
Return Value
A new image object, or
nilif the filter failed for any reason.
-
The instance image downloader used to download all images. If this property is
nil, theUIImageViewwill fallback on thesharedImageDownloaderfor all downloads. The most common use case for needing to use a custom instance image downloader is when images are behind different basic auth credentials.Declaration
Swift
public var imageDownloader: ImageDownloader? { get nonmutating set } -
The shared image downloader used to download all images. By default, this is the default
ImageDownloaderinstance backed with anAutoPurgingImageCachewhich automatically evicts images from the cache when the memory capacity is reached or memory warning notifications occur. The shared image downloader is only used if theimageDownloaderisnil.Declaration
Swift
public static var sharedImageDownloader: ImageDownloader { get set }
-
setImage(withURL:cacheKey: placeholderImage: serializer: filter: progress: progressQueue: imageTransition: runImageTransitionIfCached: completion: ) Asynchronously downloads an image from the specified URL, applies the specified image filter to the downloaded image and sets it once finished while executing the image transition.
If the image is cached locally, the image is set immediately. Otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the image request is finished.
The
completionclosure is called after the image download and filtering are complete, but before the start of the image transition. Please note it is no longer the responsibility of thecompletionclosure to set the image. It will be set automatically. If you require a second notification after the image transition completes, use a.Customimage transition with acompletionclosure. The.Customcompletionclosure is called when the image transition is finished.Declaration
Swift
public func setImage(withURL url: URL, cacheKey: String? = nil, placeholderImage: UIImage? = nil, serializer: ImageResponseSerializer? = nil, filter: ImageFilter? = nil, progress: ImageDownloader.ProgressHandler? = nil, progressQueue: DispatchQueue = DispatchQueue.main, imageTransition: UIImageView.ImageTransition = .noTransition, runImageTransitionIfCached: Bool = false, completion: ((AFIDataResponse<UIImage>) -> Void)? = nil)Parameters
urlThe URL used for the image request.
cacheKeyAn optional key used to identify the image in the cache. Defaults to
nil.placeholderImageThe image to be set initially until the image request finished. If
nil, the image view will not change its image until the image request finishes. Defaults tonil.serializerImage response serializer used to convert the image data to
UIImage. Defaults tonilwhich will fall back to the instanceimageResponseSerializerset on theImageDownloader.filterThe image filter applied to the image after the image request is finished. Defaults to
nil.progressThe closure to be executed periodically during the lifecycle of the request. Defaults to
nil.progressQueueThe dispatch queue to call the progress closure on. Defaults to the main queue.
imageTransitionThe image transition animation applied to the image when set. Defaults to
.None.runImageTransitionIfCachedWhether to run the image transition if the image is cached. Defaults to
false.completionA closure to be executed when the image request finishes. The closure has no return value and takes three arguments: the original request, the response from the server and the result containing either the image or the error that occurred. If the image was returned from the image cache, the response will be
nil. Defaults tonil. -
setImage(withURLRequest:cacheKey: placeholderImage: serializer: filter: progress: progressQueue: imageTransition: runImageTransitionIfCached: completion: ) Asynchronously downloads an image from the specified URL Request, applies the specified image filter to the downloaded image and sets it once finished while executing the image transition.
If the image is cached locally, the image is set immediately. Otherwise the specified placeholder image will be set immediately, and then the remote image will be set once the image request is finished.
The
completionclosure is called after the image download and filtering are complete, but before the start of the image transition. Please note it is no longer the responsibility of thecompletionclosure to set the image. It will be set automatically. If you require a second notification after the image transition completes, use a.Customimage transition with acompletionclosure. The.Customcompletionclosure is called when the image transition is finished.Declaration
Swift
public func setImage(withURLRequest urlRequest: URLRequestConvertible, cacheKey: String? = nil, placeholderImage: UIImage? = nil, serializer: ImageResponseSerializer? = nil, filter: ImageFilter? = nil, progress: ImageDownloader.ProgressHandler? = nil, progressQueue: DispatchQueue = DispatchQueue.main, imageTransition: UIImageView.ImageTransition = .noTransition, runImageTransitionIfCached: Bool = false, completion: ((AFIDataResponse<UIImage>) -> Void)? = nil)Parameters
urlRequestThe URL request.
cacheKeyAn optional key used to identify the image in the cache. Defaults to
nil.placeholderImageThe image to be set initially until the image request finished. If
nil, the image view will not change its image until the image request finishes. Defaults tonil.serializerImage response serializer used to convert the image data to
UIImage. Defaults tonilwhich will fall back to the instanceimageResponseSerializerset on theImageDownloader.filterThe image filter applied to the image after the image request is finished. Defaults to
nil.progressThe closure to be executed periodically during the lifecycle of the request. Defaults to
nil.progressQueueThe dispatch queue to call the progress closure on. Defaults to the main queue.
imageTransitionThe image transition animation applied to the image when set. Defaults to
.None.runImageTransitionIfCachedWhether to run the image transition if the image is cached. Defaults to
false.completionA closure to be executed when the image request finishes. The closure has no return value and takes three arguments: the original request, the response from the server and the result containing either the image or the error that occurred. If the image was returned from the image cache, the response will be
nil. Defaults tonil.
-
Cancels the active download request, if one exists.
Declaration
Swift
public func cancelImageRequest()
-
Runs the image transition on the image view with the specified image.
Declaration
Swift
public func run(_ imageTransition: UIImageView.ImageTransition, with image: Image)Parameters
imageTransitionThe image transition to ran on the image view.
imageThe image to use for the image transition.
View on GitHub
Install in Dash
AlamofireExtension Extension Reference