首页 > 其他分享 >AsyncImage

AsyncImage

时间:2024-11-01 14:59:08浏览次数:2  
标签:URL image content AsyncImage url placeholder

Structure

AsyncImage

A view that asynchronously loads and displays an image. iOS 15.0+iPadOS 15.0+Mac Catalyst 15.0+macOS 12.0+tvOS 15.0+visionOS 1.0+watchOS 8.0+
struct AsyncImage<Content> where Content : View

Overview

This view uses the shared URLSession instance to load an image from the specified URL, and then display it. For example, you can display an icon that’s stored on a server:

AsyncImage(url: URL(string: "https://example.com/icon.png"))
    .frame(width: 200, height: 200)

Until the image loads, the view displays a standard placeholder that fills the available space. After the load completes successfully, the view updates to display the image. In the example above, the icon is smaller than the frame, and so appears smaller than the placeholder.

A diagram that shows a grey box on the left, the SwiftUI icon on the

You can specify a custom placeholder using init(url:scale:content:placeholder:). With this initializer, you can also use the content parameter to manipulate the loaded image. For example, you can add a modifier to make the loaded image resizable:

AsyncImage(url: URL(string: "https://example.com/icon.png")) { image in
    image.resizable()
} placeholder: {
    ProgressView()
}
.frame(width: 50, height: 50)

For this example, SwiftUI shows a ProgressView first, and then the image scaled to fit in the specified frame:

A diagram that shows a progress view on the left, the SwiftUI icon on the

Important

You can’t apply image-specific modifiers, like resizable(capInsets:resizingMode:), directly to an AsyncImage. Instead, apply them to the Image instance that your content closure gets when defining the view’s appearance.

To gain more control over the loading process, use the init(url:scale:transaction:content:) initializer, which takes a content closure that receives an AsyncImagePhase to indicate the state of the loading operation. Return a view that’s appropriate for the current phase:

AsyncImage(url: URL(string: "https://example.com/icon.png")) { phase in
    if let image = phase.image {
        image // Displays the loaded image.
    } else if phase.error != nil {
        Color.red // Indicates an error.
    } else {
        Color.blue // Acts as a placeholder.
    }
}

Topics

Loading an image

init(url: URL?, scale: CGFloat) Loads and displays an image from the specified URL. init<I, P>(url: URL?, scale: CGFloat, content: (Image) -> I, placeholder: () -> P) Loads and displays a modifiable image from the specified URL using a custom placeholder until the image loads.

Loading an image in phases

init(url: URL?, scale: CGFloat, transaction: Transaction, content: (AsyncImagePhase) -> Content) Loads and displays a modifiable image from the specified URL in phases.

Relationships

Conforms To

标签:URL,image,content,AsyncImage,url,placeholder
From: https://www.cnblogs.com/sexintercourse/p/18520264

相关文章

  • SwiftUI中AsyncImage的使用(一个高效的异步下载图片组件)
    iOS开发者经常会遇到需要在应用中显示网络图像的场景,无论是获取和显示用户头像,展示产品图像,等等。在原来的UIKit中,如果我们要用系统的API还是稍微有点麻烦,很多开发的朋友都选择了第三方的框架去处理网络图片的请求缓存等等。AsyncImage是SwiftUI中一个强大的功能,它简化了在......