AnimatedImage

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct AnimatedImage : PlatformViewRepresentable

A Image View type to load image from url, data or bundle. Supports animated and static image format.

  • A Binding to control the animation. You can bind external logic to control the animation status. True to start animation, false to stop animation.

    Declaration

    Swift

    @Binding
    public var isAnimating: Bool { get nonmutating set }
  • Create an animated image with url, placeholder, custom options and context.

    Declaration

    Swift

    public init(url: URL?, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil)

    Parameters

    url

    The image url

    placeholder

    The placeholder image to show during loading

    options

    The options to use when downloading the image. See SDWebImageOptions for the possible values.

    context

    A context contains different options to perform specify changes or processes, see SDWebImageContextOption. This hold the extra objects which options enum can not hold.

  • Create an animated image with url, placeholder, custom options and context, including animation control binding.

    Declaration

    Swift

    public init(url: URL?, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil, isAnimating: Binding<Bool>)

    Parameters

    url

    The image url

    placeholder

    The placeholder image to show during loading

    options

    The options to use when downloading the image. See SDWebImageOptions for the possible values.

    context

    A context contains different options to perform specify changes or processes, see SDWebImageContextOption. This hold the extra objects which options enum can not hold.

    isAnimating

    The binding for animation control

  • Create an animated image with name and bundle.

    Note

    Asset Catalog is not supported.

    Declaration

    Swift

    public init(name: String, bundle: Bundle? = nil)

    Parameters

    name

    The image name

    bundle

    The bundle contains image

  • Create an animated image with name and bundle, including animation control binding.

    Note

    Asset Catalog is not supported.

    Declaration

    Swift

    public init(name: String, bundle: Bundle? = nil, isAnimating: Binding<Bool>)

    Parameters

    name

    The image name

    bundle

    The bundle contains image

    isAnimating

    The binding for animation control

  • Create an animated image with data and scale.

    Declaration

    Swift

    public init(data: Data, scale: CGFloat = 1)

    Parameters

    data

    The image data

    scale

    The scale factor

  • Create an animated image with data and scale, including animation control binding.

    Declaration

    Swift

    public init(data: Data, scale: CGFloat = 1, isAnimating: Binding<Bool>)

    Parameters

    data

    The image data

    scale

    The scale factor

    isAnimating

    The binding for animation control

  • Declaration

    Swift

    public typealias UIViewType = AnimatedImageViewWrapper
  • Declaration

    Swift

    public typealias Coordinator = AnimatedImageCoordinator
  • Declaration

    Swift

    public func makeCoordinator() -> Coordinator
  • Declaration

    Swift

    public func makeUIView(context: UIViewRepresentableContext<AnimatedImage>) -> AnimatedImageViewWrapper
  • Declaration

    Swift

    public func updateUIView(_ uiView: AnimatedImageViewWrapper, context: UIViewRepresentableContext<AnimatedImage>)
  • Declaration

    Swift

    public static func dismantleUIView(_ uiView: AnimatedImageViewWrapper, coordinator: Coordinator)
  • Configurate this view’s image with the specified cap insets and options.

    Warning

    Animated Image does not implementes.

    Declaration

    Swift

    public func resizable(
        capInsets: EdgeInsets = EdgeInsets(),
        resizingMode: Image.ResizingMode = .stretch) -> AnimatedImage

    Parameters

    capInsets

    The values to use for the cap insets.

    resizingMode

    The resizing mode

  • Configurate this view’s rendering mode.

    Warning

    Animated Image does not implementes.

    Declaration

    Swift

    public func renderingMode(_ renderingMode: Image.TemplateRenderingMode?) -> AnimatedImage

    Parameters

    renderingMode

    The resizing mode

  • Configurate this view’s image interpolation quality

    Declaration

    Swift

    public func interpolation(_ interpolation: Image.Interpolation) -> AnimatedImage

    Parameters

    interpolation

    The interpolation quality

  • Configurate this view’s image antialiasing

    Declaration

    Swift

    public func antialiased(_ isAntialiased: Bool) -> AnimatedImage

    Parameters

    isAntialiased

    Whether or not to allow antialiasing

  • Constrains this view’s dimensions to the specified aspect ratio.

    Declaration

    Swift

    public func aspectRatio(_ aspectRatio: CGFloat? = nil, contentMode: ContentMode) -> some View

    Parameters

    aspectRatio

    The ratio of width to height to use for the resulting view. If aspectRatio is nil, the resulting view maintains this view’s aspect ratio.

    contentMode

    A flag indicating whether this view should fit or fill the parent context.

    Return Value

    A view that constrains this view’s dimensions to aspectRatio, using contentMode as its scaling algorithm.

  • Constrains this view’s dimensions to the aspect ratio of the given size.

    Declaration

    Swift

    public func aspectRatio(_ aspectRatio: CGSize, contentMode: ContentMode) -> some View

    Parameters

    aspectRatio

    A size specifying the ratio of width to height to use for the resulting view.

    contentMode

    A flag indicating whether this view should fit or fill the parent context.

    Return Value

    A view that constrains this view’s dimensions to aspectRatio, using contentMode as its scaling algorithm.

  • Scales this view to fit its parent.

    Declaration

    Swift

    public func scaledToFit() -> some View

    Return Value

    A view that scales this view to fit its parent, maintaining this view’s aspect ratio.

  • Scales this view to fill its parent.

    Declaration

    Swift

    public func scaledToFill() -> some View

    Return Value

    A view that scales this view to fit its parent, maintaining this view’s aspect ratio.

  • Total loop count for animated image rendering. Defaults to nil.

    Note

    Pass nil to disable customization, use the image itself loop count (animatedImageLoopCount) instead

    Declaration

    Swift

    public func customLoopCount(_ loopCount: UInt?) -> AnimatedImage

    Parameters

    loopCount

    The animation loop count

  • Provide a max buffer size by bytes. This is used to adjust frame buffer count and can be useful when the decoding cost is expensive (such as Animated WebP software decoding). Default is nil.

    0 or nil means automatically adjust by calculating current memory usage. 1 means without any buffer cache, each of frames will be decoded and then be freed after rendering. (Lowest Memory and Highest CPU) UInt.max means cache all the buffer. (Lowest CPU and Highest Memory)

    Declaration

    Swift

    public func maxBufferSize(_ bufferSize: UInt?) -> AnimatedImage

    Parameters

    bufferSize

    The max buffer size

  • Whehter or not to enable incremental image load for animated image. See SDAnimatedImageView for detailed explanation for this.

    Note

    If you are confused about this description, open Chrome browser to view some large GIF images with low network speed to see the animation behavior. Default is true. Set to false to only render the static poster for incremental animated image.

    Declaration

    Swift

    public func incrementalLoad(_ incrementalLoad: Bool) -> AnimatedImage

    Parameters

    incrementalLoad

    Whether or not to incremental load

  • The runLoopMode when animation is playing on. Defaults is .common You can specify a runloop mode to let it rendering.

    Note

    This is useful for some cases, for example, always specify NSDefaultRunLoopMode, if you want to pause the animation when user scroll (for Mac user, drag the mouse or touchpad)

    Declaration

    Swift

    public func runLoopMode(_ runLoopMode: RunLoop.Mode) -> AnimatedImage

    Parameters

    runLoopMode

    The runLoopMode for animation

  • Whether or not to pause the animation (keep current frame), instead of stop the animation (frame index reset to 0). When isAnimating binding value changed to false. Defaults is true.

    Note

    For some of use case, you may want to reset the frame index to 0 when stop, but some other want to keep the current frame index.

    Declaration

    Swift

    public func pausable(_ pausable: Bool) -> AnimatedImage

    Parameters

    pausable

    Whether or not to pause the animation instead of stop the animation.

  • Whether or not to clear frame buffer cache when stopped. Defaults is false. Note: This is useful when you want to limit the memory usage during frequently visibility changes (such as image view inside a list view, then push and pop)

    Declaration

    Swift

    public func purgeable(_ purgeable: Bool) -> AnimatedImage

    Parameters

    purgeable

    Whether or not to clear frame buffer cache when stopped.

  • Control the animation playback rate. Default is 1.0. 1.0 means the normal speed. 0.0 means stopping the animation. 0.0-1.0 means the slow speed. > 1.0 means the fast speed. < 0.0 is not supported currently and stop animation. (may support reverse playback in the future)

    Declaration

    Swift

    public func playbackRate(_ playbackRate: Double) -> AnimatedImage

    Parameters

    playbackRate

    The animation playback rate.

  • Provide the action when image load fails.

    Declaration

    Swift

    public func onFailure(perform action: ((Error) -> Void)? = nil) -> AnimatedImage

    Parameters

    action

    The action to perform. The first arg is the error during loading. If action is nil, the call has no effect.

    Return Value

    A view that triggers action when this image load fails.

  • Provide the action when image load successes.

    Declaration

    Swift

    public func onSuccess(perform action: @escaping (PlatformImage) -> Void) -> AnimatedImage

    Parameters

    action

    The action to perform. The first arg is the loaded image. If action is nil, the call has no effect.

    Return Value

    A view that triggers action when this image load successes.

  • Provide the action when image load successes.

    Declaration

    Swift

    public func onSuccess(perform action: @escaping (PlatformImage, SDImageCacheType) -> Void) -> AnimatedImage

    Parameters

    action

    The action to perform. The first arg is the loaded image, the second arg is the cache type loaded from. If action is nil, the call has no effect.

    Return Value

    A view that triggers action when this image load successes.

  • Provide the action when image load successes.

    Declaration

    Swift

    public func onSuccess(perform action: ((PlatformImage, Data?, SDImageCacheType) -> Void)? = nil) -> AnimatedImage

    Parameters

    action

    The action to perform. The first arg is the loaded image, the second arg is the loaded image data, the third arg is the cache type loaded from. If action is nil, the call has no effect.

    Return Value

    A view that triggers action when this image load successes.

  • Provide the action when image load progress changes.

    Declaration

    Swift

    public func onProgress(perform action: ((Int, Int) -> Void)? = nil) -> AnimatedImage

    Parameters

    action

    The action to perform. The first arg is the received size, the second arg is the total size, all in bytes. If action is nil, the call has no effect.

    Return Value

    A view that triggers action when this image load successes.

  • Provide the action when view representable create the native view.

    Declaration

    Swift

    public func onViewCreate(perform action: ((PlatformView, Context) -> Void)? = nil) -> AnimatedImage

    Parameters

    action

    The action to perform. The first arg is the native view. The seconds arg is the context.

    Return Value

    A view that triggers action when view representable create the native view.

  • Provide the action when view representable update the native view.

    Declaration

    Swift

    public func onViewUpdate(perform action: ((PlatformView, Context) -> Void)? = nil) -> AnimatedImage

    Parameters

    action

    The action to perform. The first arg is the native view. The seconds arg is the context.

    Return Value

    A view that triggers action when view representable update the native view.

  • Provide the action when view representable destroy the native view

    Declaration

    Swift

    public static func onViewDestroy(perform action: ((PlatformView, Coordinator) -> Void)? = nil)

    Parameters

    action

    The action to perform. The first arg is the native view. The seconds arg is the coordinator (with userInfo).

    Return Value

    A view that triggers action when view representable destroy the native view.

  • Associate a placeholder when loading image with url

    Note

    The differences between this and placeholder image, it’s that placeholder image replace the image for image view, but this modify the View Hierarchy to overlay the placeholder hosting view

    Declaration

    Swift

    public func placeholder<T>(@ViewBuilder content: () -> T) -> AnimatedImage where T : View

    Parameters

    content

    A view that describes the placeholder.

  • Associate a placeholder image when loading image with url

    Declaration

    Swift

    public func placeholder(_ image: PlatformImage?) -> AnimatedImage

    Parameters

    content

    A view that describes the placeholder.

  • Associate a indicator when loading image with url

    Note

    If you do not need indicator, specify nil. Defaults to nil

    Declaration

    Swift

    public func indicator(_ indicator: SDWebImageIndicator?) -> AnimatedImage

    Parameters

    indicator

    indicator, see more in SDWebImageIndicator

  • Associate a transition when loading image with url

    Note

    If you specify nil, do not do transition. Defautls to nil.

    Declaration

    Swift

    public func transition(_ transition: SDWebImageTransition?) -> AnimatedImage

    Parameters

    transition

    transition, see more in SDWebImageTransition

  • Associate a indicator when loading image with url

    Declaration

    Swift

    public func indicator<T>(_ indicator: Indicator<T>) -> some View where T : View

    Parameters

    indicator

    The indicator type, see Indicator

  • Associate a indicator when loading image with url, convenient method with block

    Declaration

    Swift

    public func indicator<T>(@ViewBuilder content: @escaping (_ isAnimating: Binding<Bool>, _ progress: Binding<Double>) -> T) -> some View where T : View

    Parameters

    content

    A view that describes the indicator.