Commit b2f471d2 authored by shenyong's avatar shenyong

优化图片加载器

parent 560ec868
//
// IAPPageManager.swift
// PhoneManager
//
// Created by edy on 2025/5/19.
//
import Foundation
struct IAPPageManager{
func showIAPPage(){
}
}
......@@ -2,7 +2,12 @@ import UIKit
import Photos
// 图片缓存管理(单例)
private let imageCache = NSCache<NSString, UIImage>()
private let imageCache: NSCache<NSString, UIImage> = {
let cache = NSCache<NSString, UIImage>()
cache.totalCostLimit = 50 * 1024 * 1024 // 50MB
cache.countLimit = 100 // 最多缓存100张图片
return cache
}()
extension UIImageView {
// 关联对象键,用于存储当前请求的 localIdentifier
......@@ -34,7 +39,7 @@ extension UIImageView {
// 加载图片的主方法
func loadImage(
withLocalIdentifier identifier: String,
targetSize: CGSize = PHImageManagerMaximumSize,
targetSize: CGSize = CGSize(width: 300, height: 300),
placeholder: UIImage? = nil,
completion: ((UIImage?) -> Void)? = nil
) {
......@@ -63,13 +68,23 @@ extension UIImageView {
completion?(nil)
return
}
// 6. 配置图片请求选项
let options = PHImageRequestOptions()
options.version = .current
options.deliveryMode = .highQualityFormat
options.deliveryMode = .highQualityFormat // 改为渐进式加载
options.isNetworkAccessAllowed = true
options.resizeMode = .exact
options.resizeMode = .fast // 改为快速加载模式
options.isSynchronous = false
options.progressHandler = { progress, error, stop, info in
DispatchQueue.main.async {
// 可以在这里添加加载进度的处理
print("Loading progress: \(progress)")
if let error = error {
print("Loading error: \(error.localizedDescription)")
}
}
}
// 7. 请求图片
currentRequestID = PHImageManager.default().requestImage(
......@@ -116,9 +131,18 @@ extension UIImageView {
}
}
// 清理缓存
@objc func clearCacheOnMemoryWarning() {
imageCache.removeAllObjects()
}
// 命名空间
struct AssetLoader {
fileprivate weak var imageView: UIImageView?
func clearImageCache() {
imageCache.removeAllObjects()
}
}
var asset: AssetLoader {
......
......@@ -34,6 +34,12 @@ enum BaseDataLoadingState {
case failed(Error)
}
enum FileType {
case json
case plist
case all
}
class PhotoManager{
......@@ -142,7 +148,7 @@ class PhotoManager{
if permission{
weakSelf.getBaseAssetGroup()
}else{
weakSelf.clearLocalData()
//weakSelf.clearLocalData()
}
}
......@@ -753,6 +759,7 @@ extension PhotoManager{
}
}
// 清楚本地存储数据
func clearLocalData(){
}
......
......@@ -121,6 +121,10 @@ class HomeViewModel {
return
}
guard photoManager.permissionStatus == .authorized else{
return
}
totalFilesCount = photoManager.allAssets.count
hadLoad = true
......
......@@ -70,4 +70,8 @@ struct FileTool{
return totalSize
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment