WebImage

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

A Image View type to load image from url. Supports static/animated 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 a web 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

    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 a web image with url, placeholder, custom options and context. Optional can support animated image using Binding.

    Declaration

    Swift

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

    Parameters

    url

    The image url

    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. The binding value should be true when initialized to setup the correct animated image class. If not, you must provide the .animatedImageClass explicitly. When the animation started, this binding can been used to start / stop the animation.

  • Declaration

    Swift

    public var body: some View { get }
  • Configurate this view’s image with the specified cap insets and options.

    Declaration

    Swift

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

    Parameters

    capInsets

    The values to use for the cap insets.

    resizingMode

    The resizing mode

  • Configurate this view’s rendering mode.

    Declaration

    Swift

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

    Parameters

    renderingMode

    The resizing mode

  • Configurate this view’s image interpolation quality

    Declaration

    Swift

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

    Parameters

    interpolation

    The interpolation quality

  • Configurate this view’s image antialiasing

    Declaration

    Swift

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

    Parameters

    isAntialiased

    Whether or not to allow antialiasing

  • Provide the action when image load fails.

    Declaration

    Swift

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

    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) -> WebImage

    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) -> WebImage

    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) -> WebImage

    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) -> WebImage

    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.

  • Associate a placeholder when loading image with url

    Note

    The differences between Placeholder and Indicator, is that placeholder does not supports animation, and return type is different

    Declaration

    Swift

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

    Parameters

    content

    A view that describes the placeholder.

  • Associate a placeholder image when loading image with url

    Note

    This placeholder image will apply the same size and resizable from WebImage for convenience. If you don’t want this, use the ViewBuilder one above instead

    Declaration

    Swift

    public func placeholder(_ image: Image) -> WebImage

    Parameters

    image

    A Image view that describes the placeholder.

  • Control the behavior to retry the failed loading when view become appears again

    Declaration

    Swift

    public func retryOnAppear(_ flag: Bool) -> WebImage

    Parameters

    flag

    Whether or not to retry the failed loading

  • Control the behavior to cancel the pending loading when view become disappear again

    Declaration

    Swift

    public func cancelOnDisappear(_ flag: Bool) -> WebImage

    Parameters

    flag

    Whether or not to cancel the pending loading

  • 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.

  • 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?) -> WebImage

    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?) -> WebImage

    Parameters

    bufferSize

    The max buffer size

  • 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) -> WebImage

    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) -> WebImage

    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) -> WebImage

    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) -> WebImage

    Parameters

    playbackRate

    The animation playback rate.