AlamofireExtension

extension AlamofireExtension where ExtendedType: UIButton
extension AlamofireExtension where ExtendedType: UIImage
extension AlamofireExtension where ExtendedType: UIImageView

Properties

  • The instance image downloader used to download all images. If this property is nil, the UIButton will fallback on the sharedImageDownloader for 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 ImageDownloader instance backed with an AutoPurgingImageCache which 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 the imageDownloader is nil.

    Declaration

    Swift

    public static var sharedImageDownloader: ImageDownloader { get set }

Image Downloads

  • 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

    state

    The control state of the button to set the image on.

    url

    The URL used for your image request.

    cacheKey

    An optional key used to identify the image in the cache. Defaults to nil.

    placeholderImage

    The 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 to nil.

    serializer

    Image response serializer used to convert the image data to UIImage. Defaults to nil which will fall back to the instance imageResponseSerializer set on the ImageDownloader.

    filter

    The image filter applied to the image after the image request is finished. Defaults to nil.

    progress

    The closure to be executed periodically during the lifecycle of the request. Defaults to nil.

    progressQueue

    The dispatch queue to call the progress closure on. Defaults to the main queue.

    completion

    A 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 to nil.

  • 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

    state

    The control state of the button to set the image on.

    urlRequest

    The URL request.

    cacheKey

    An optional key used to identify the image in the cache. Defaults to nil.

    placeholderImage

    The 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 to nil.

    serializer

    Image response serializer used to convert the image data to UIImage. Defaults to nil which will fall back to the instance imageResponseSerializer set on the ImageDownloader.

    filter

    The image filter applied to the image after the image request is finished. Defaults to nil.

    progress

    The closure to be executed periodically during the lifecycle of the request. Defaults to nil.

    progressQueue

    The dispatch queue to call the progress closure on. Defaults to the main queue.

    completion

    A 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 to nil.

  • Cancels the active download request for the image, if one exists.

    Declaration

    Swift

    public func cancelImageRequest(for state: ControlState)

Background Image Downloads

  • 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

    state

    The control state of the button to set the image on.

    url

    The URL used for the image request.

    cacheKey

    An optional key used to identify the image in the cache. Defaults to nil.

    placeholderImage

    The 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 to nil.

    serializer

    Image response serializer used to convert the image data to UIImage. Defaults to nil which will fall back to the instance imageResponseSerializer set on the ImageDownloader.

    filter

    The image filter applied to the image after the image request is finished. Defaults to nil.

    progress

    The closure to be executed periodically during the lifecycle of the request. Defaults to nil.

    progressQueue

    The dispatch queue to call the progress closure on. Defaults to the main queue.

    completion

    A 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 to nil.

  • 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

    state

    The control state of the button to set the image on.

    urlRequest

    The URL request.

    cacheKey

    An optional key used to identify the image in the cache. Defaults to nil.

    placeholderImage

    The 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 to nil.

    serializer

    Image response serializer used to convert the image data to UIImage. Defaults to nil which will fall back to the instance imageResponseSerializer set on the ImageDownloader.

    filter

    The image filter applied to the image after the image request is finished. Defaults to nil.

    progress

    The closure to be executed periodically during the lifecycle of the request. Defaults to nil.

    progressQueue

    The dispatch queue to call the progress closure on. Defaults to the main queue.

    completion

    A 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 to nil.

  • Cancels the active download request for the background image, if one exists.

    Declaration

    Swift

    public func cancelBackgroundImageRequest(for state: ControlState)

Available where ExtendedType: UIImage

  • 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

    data

    The data object containing the image data.

    Return Value

    An initialized UIImage object, or nil if 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

    data

    The data object containing the image data.

    scale

    The 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 UIImage object, or nil if 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) -> UIImage

    Parameters

    size

    The size to use when scaling the new image.

    scale

    The scale to set for the new image. Defaults to nil which 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 scaledSize manually, then use the af.imageScaledToSize method in conjunction with a .Center content mode to achieve the same visual result.

    Declaration

    Swift

    public func imageAspectScaled(toFit size: CGSize, scale: CGFloat? = nil) -> UIImage

    Parameters

    size

    The size to use when scaling the new image.

    scale

    The scale to set for the new image. Defaults to nil which 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) -> UIImage

    Parameters

    size

    The size to use when scaling the new image.

    scale

    The scale to set for the new image. Defaults to nil which 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) -> UIImage

    Parameters

    radius

    The radius to use when rounding the new image.

    divideRadiusByImageScale

    Whether to divide the radius by the image scale. Set to true when the image has the same resolution for all screen scales such as @1x, @2x and @3x (i.e. single image from web server). Set to false for images loaded from an asset catalog with varying resolutions for each screen scale. false by default.

    Return Value

    A new image object.

  • Returns a new version of the image rounded into a circle.

    Declaration

    Swift

    public func imageRoundedIntoCircle() -> UIImage

    Return 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

    name

    The name of the CoreImage filter to use on the new image.

    parameters

    The parameters to apply to the CoreImage filter.

    Return Value

    A new image object, or nil if the filter failed for any reason.

Properties

  • The instance image downloader used to download all images. If this property is nil, the UIImageView will fallback on the sharedImageDownloader for 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 ImageDownloader instance backed with an AutoPurgingImageCache which 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 the imageDownloader is nil.

    Declaration

    Swift

    public static var sharedImageDownloader: ImageDownloader { get set }

Image Download

  • 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 completion closure 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 the completion closure to set the image. It will be set automatically. If you require a second notification after the image transition completes, use a .Custom image transition with a completion closure. The .Custom completion closure 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

    url

    The URL used for the image request.

    cacheKey

    An optional key used to identify the image in the cache. Defaults to nil.

    placeholderImage

    The 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 to nil.

    serializer

    Image response serializer used to convert the image data to UIImage. Defaults to nil which will fall back to the instance imageResponseSerializer set on the ImageDownloader.

    filter

    The image filter applied to the image after the image request is finished. Defaults to nil.

    progress

    The closure to be executed periodically during the lifecycle of the request. Defaults to nil.

    progressQueue

    The dispatch queue to call the progress closure on. Defaults to the main queue.

    imageTransition

    The image transition animation applied to the image when set. Defaults to .None.

    runImageTransitionIfCached

    Whether to run the image transition if the image is cached. Defaults to false.

    completion

    A 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 to nil.

  • 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 completion closure 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 the completion closure to set the image. It will be set automatically. If you require a second notification after the image transition completes, use a .Custom image transition with a completion closure. The .Custom completion closure 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

    urlRequest

    The URL request.

    cacheKey

    An optional key used to identify the image in the cache. Defaults to nil.

    placeholderImage

    The 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 to nil.

    serializer

    Image response serializer used to convert the image data to UIImage. Defaults to nil which will fall back to the instance imageResponseSerializer set on the ImageDownloader.

    filter

    The image filter applied to the image after the image request is finished. Defaults to nil.

    progress

    The closure to be executed periodically during the lifecycle of the request. Defaults to nil.

    progressQueue

    The dispatch queue to call the progress closure on. Defaults to the main queue.

    imageTransition

    The image transition animation applied to the image when set. Defaults to .None.

    runImageTransitionIfCached

    Whether to run the image transition if the image is cached. Defaults to false.

    completion

    A 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 to nil.

Image Download Cancellation

  • Cancels the active download request, if one exists.

    Declaration

    Swift

    public func cancelImageRequest()

Image Transition

  • Runs the image transition on the image view with the specified image.

    Declaration

    Swift

    public func run(_ imageTransition: UIImageView.ImageTransition, with image: Image)

    Parameters

    imageTransition

    The image transition to ran on the image view.

    image

    The image to use for the image transition.