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 ...@@ -2,7 +2,12 @@ import UIKit
import Photos 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 { extension UIImageView {
// 关联对象键,用于存储当前请求的 localIdentifier // 关联对象键,用于存储当前请求的 localIdentifier
...@@ -34,7 +39,7 @@ extension UIImageView { ...@@ -34,7 +39,7 @@ extension UIImageView {
// 加载图片的主方法 // 加载图片的主方法
func loadImage( func loadImage(
withLocalIdentifier identifier: String, withLocalIdentifier identifier: String,
targetSize: CGSize = PHImageManagerMaximumSize, targetSize: CGSize = CGSize(width: 300, height: 300),
placeholder: UIImage? = nil, placeholder: UIImage? = nil,
completion: ((UIImage?) -> Void)? = nil completion: ((UIImage?) -> Void)? = nil
) { ) {
...@@ -63,13 +68,23 @@ extension UIImageView { ...@@ -63,13 +68,23 @@ extension UIImageView {
completion?(nil) completion?(nil)
return return
} }
// 6. 配置图片请求选项 // 6. 配置图片请求选项
let options = PHImageRequestOptions() let options = PHImageRequestOptions()
options.version = .current options.version = .current
options.deliveryMode = .highQualityFormat options.deliveryMode = .highQualityFormat // 改为渐进式加载
options.isNetworkAccessAllowed = true 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. 请求图片 // 7. 请求图片
currentRequestID = PHImageManager.default().requestImage( currentRequestID = PHImageManager.default().requestImage(
...@@ -116,9 +131,18 @@ extension UIImageView { ...@@ -116,9 +131,18 @@ extension UIImageView {
} }
} }
// 清理缓存
@objc func clearCacheOnMemoryWarning() {
imageCache.removeAllObjects()
}
// 命名空间 // 命名空间
struct AssetLoader { struct AssetLoader {
fileprivate weak var imageView: UIImageView? fileprivate weak var imageView: UIImageView?
func clearImageCache() {
imageCache.removeAllObjects()
}
} }
var asset: AssetLoader { var asset: AssetLoader {
......
...@@ -34,6 +34,12 @@ enum BaseDataLoadingState { ...@@ -34,6 +34,12 @@ enum BaseDataLoadingState {
case failed(Error) case failed(Error)
} }
enum FileType {
case json
case plist
case all
}
class PhotoManager{ class PhotoManager{
...@@ -142,7 +148,7 @@ class PhotoManager{ ...@@ -142,7 +148,7 @@ class PhotoManager{
if permission{ if permission{
weakSelf.getBaseAssetGroup() weakSelf.getBaseAssetGroup()
}else{ }else{
weakSelf.clearLocalData() //weakSelf.clearLocalData()
} }
} }
...@@ -753,6 +759,7 @@ extension PhotoManager{ ...@@ -753,6 +759,7 @@ extension PhotoManager{
} }
} }
// 清楚本地存储数据
func clearLocalData(){ func clearLocalData(){
} }
......
...@@ -121,6 +121,10 @@ class HomeViewModel { ...@@ -121,6 +121,10 @@ class HomeViewModel {
return return
} }
guard photoManager.permissionStatus == .authorized else{
return
}
totalFilesCount = photoManager.allAssets.count totalFilesCount = photoManager.allAssets.count
hadLoad = true hadLoad = true
......
...@@ -70,4 +70,8 @@ struct FileTool{ ...@@ -70,4 +70,8 @@ struct FileTool{
return totalSize 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