fix image view

This commit is contained in:
anlicheng 2025-04-14 11:16:52 +08:00
parent 91718319f8
commit 4fb871ddb9
3 changed files with 112 additions and 44 deletions

View File

@ -37,6 +37,18 @@ final class CacheManager {
}
}
//
func preloadImage(url: String) async {
guard !self.fileExists(urlString: url) else {
return
}
if let filename = self.getCacheFileName(urlString: url),
let data = try? await self.downloadImage(from: url) {
try? self.saveCacheFile(filename: filename, data: data)
}
}
//
func fileExists(urlString: String) -> Bool {
guard let cacheFileName = getCacheFileName(urlString: urlString) else {

View File

@ -0,0 +1,73 @@
//
// FlexImage.swift
// dimensionhub
//
// Created by on 2025/4/14.
//
import SwiftUI
struct FlexImage: View {
let urlString: String
let width: CGFloat
let height: CGFloat
let placeholder: String
let mode: ImageMode
//
enum ImageMode {
case remote(String)
case local(Data)
}
init(urlString: String, width: CGFloat, height: CGFloat, placeholder: String) {
self.urlString = urlString
self.width = width
self.height = height
self.placeholder = placeholder
let cacheManager = CacheManager.shared
if let data = cacheManager.readFileContents(urlString: urlString) {
self.mode = .local(data)
} else {
Task.detached {
await cacheManager.preloadImage(url: urlString)
}
self.mode = .remote(urlString)
}
}
var body: some View {
switch self.mode {
case .remote(let url):
AsyncImage(url: URL(string: url)) { phase in
switch phase {
case .empty:
ProgressView()
case .success(let image):
image
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: width, height: height)
.clipped()
default:
Image(placeholder)
.resizable()
.aspectRatio(contentMode: .fill)
.clipped()
}
}
case .local(let data):
Image(uiImage: UIImage(data: data)!)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: width, height: height)
.clipped()
}
}
}
#Preview {
//FlexImage()
}

View File

@ -189,24 +189,7 @@ extension IndexMainView {
Button(action: {
appNav.append(dest: .detail(id: item.id))
}) {
AsyncImage(url: URL(string: item.thumb)) { phase in
switch phase {
case .empty:
ProgressView()
case .success(let image):
image
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 370, height: 180)
.clipped()
default:
Image("ph_img_big")
.resizable()
.aspectRatio(contentMode: .fill)
.clipped()
}
}
FlexImage(urlString: item.thumb, width: 370, height: 180, placeholder: "ph_img_big")
.frame(width: 370, height: 180)
.overlay(alignment: .topLeading) {
VStack(alignment: .leading, spacing: 8) {