Commit 4608b52c authored by CZ1004's avatar CZ1004

修改bug,详情页调试

parent 105c2f12
...@@ -18,6 +18,9 @@ class HomeViewController:BaseViewController { ...@@ -18,6 +18,9 @@ class HomeViewController:BaseViewController {
super.viewDidLoad() super.viewDidLoad()
let dataUpdated = Notification.Name("DataUpdatedNotification")
NotificationCenter.default.addObserver(self, selector: #selector(handleDataUpdated(_:)), name: dataUpdated, object: nil)
view.backgroundColor = .green view.backgroundColor = .green
homeView = HomeView(frame: view.bounds) homeView = HomeView(frame: view.bounds)
...@@ -175,4 +178,48 @@ class HomeViewController:BaseViewController { ...@@ -175,4 +178,48 @@ class HomeViewController:BaseViewController {
} }
} }
@objc func handleDataUpdated(_ notification: Notification) {
if let userInfo = notification.userInfo {
let type : PhotsFileType = userInfo["type"] as! PhotsFileType
let deleteIndex : [Int] = (userInfo["deleteIndex"] as? [Int])!
for item in deleteIndex {
switch type {
case .duplicates :
self.homeView?.model?.titleModelArray.first?.assets.remove(at: item)
break
case .similar :
self.homeView?.model?.titleModelArray[1].assets.remove(at: item)
break
case .similarScreenshots:
self.homeView?.model?.otherModelArray[1].assets.remove(at: item)
break
case .SimilarVideos:
self.homeView?.model?.titleModelArray[3].assets.remove(at: item)
break
case .videos:
break
case .screenshots:
break
case .Other:
break
}
}
// 更新缓存数据
if self.homeView?.model != nil{
PhotoDataManager.manager.saveToFileSystem(model: (self.homeView?.model)!)
}
DispatchQueue.main.async {
self.homeView?.collectionView.reloadData()
}
} else {
print("接收到数据更新通知,但没有携带额外信息")
}
}
} }
...@@ -184,11 +184,31 @@ class HomeInfoView :UIView{ ...@@ -184,11 +184,31 @@ class HomeInfoView :UIView{
func deleteModel() { func deleteModel() {
var modelRecord : [Int] = []
for i in 0..<models.count { for i in 0..<models.count {
// 过滤掉 isSelected 为 true 的 smodel2 // 过滤掉 isSelected 为 true 的 smodel2
let array : [ImageSeletedCollectionItem] = models[i].smodels!
for item in array {
if item.isSeleted == true {
modelRecord.append(i)
break
}
}
models[i].smodels = models[i].smodels?.filter { !($0.isSeleted ?? false) } models[i].smodels = models[i].smodels?.filter { !($0.isSeleted ?? false) }
} }
var modelRecordDelete : [Int] = []
for index in modelRecord {
if models[index].smodels!.count < 2 {
models.remove(at: index)
modelRecordDelete.append(index)
}
}
// 更新下首页数据以及缓存数据
let dataUpdated = Notification.Name("DataUpdatedNotification")
let userInfo = ["type": self.type as Any,"deleteIndex" : modelRecordDelete] as [String : Any]
NotificationCenter.default.post(name: dataUpdated, object: nil, userInfo: userInfo)
DispatchQueue.main.async {[weak self] in DispatchQueue.main.async {[weak self] in
guard let self else {return} guard let self else {return}
......
...@@ -217,6 +217,18 @@ extension HomeInfoTableViewCell:UICollectionViewDelegate,UICollectionViewDataSou ...@@ -217,6 +217,18 @@ extension HomeInfoTableViewCell:UICollectionViewDelegate,UICollectionViewDataSou
return 10 // 设置列间距 return 10 // 设置列间距
} }
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// 点击之后跳转详情页面
let vc = PMShowImgVideoController()
if model?.type == .SimilarVideos || model?.type == .videos {
vc.state = .similarVideos
}else{
vc.state = .similarPhotos
}
vc.currentIdx = indexPath.row
vc.homeDataSource = self.model?.smodels
self.responderViewController()?.navigationController?.pushViewController(vc, animated: true)
}
} }
......
...@@ -13,8 +13,17 @@ class PMShowVideoCell: UICollectionViewCell { ...@@ -13,8 +13,17 @@ class PMShowVideoCell: UICollectionViewCell {
var playURL:String = "" var playURL:String = ""
var homeResouceUrl : URL?
var type : Int = 0
func reload() -> Void { func reload() -> Void {
let url:URL = "SecretIm".document().appendingPathComponent(playURL) var url : URL
if type == 0 {
url = "SecretIm".document().appendingPathComponent(playURL)
}else{
url = homeResouceUrl!
}
player.playVideo(from: url) player.playVideo(from: url)
player.pause() player.pause()
} }
......
...@@ -6,15 +6,28 @@ ...@@ -6,15 +6,28 @@
// //
import UIKit import UIKit
import Photos
class PMShowImgVideoController: BaseViewController { class PMShowImgVideoController: BaseViewController {
enum ShowState { enum ShowState {
case secret case secret
// 相似图片
case similarPhotos
// 相似视频
case similarVideos
} }
// 资源类型 0-图片 1-视频
var homeMediaType : Int = 0
var state:ShowState = .secret var state:ShowState = .secret
var imageVideoPath:[String] = [] var imageVideoPath:[String] = []
// 首页过来的数据
var homeDataSource : [ImageSeletedCollectionItem]?
var currentIdx = 0 var currentIdx = 0
override func viewDidLoad() { override func viewDidLoad() {
...@@ -106,7 +119,12 @@ extension PMShowImgVideoController : UICollectionViewDelegate,UICollectionViewDa ...@@ -106,7 +119,12 @@ extension PMShowImgVideoController : UICollectionViewDelegate,UICollectionViewDa
} }
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageVideoPath.count if self.state == .secret {
return imageVideoPath.count
}else {
return self.homeDataSource?.count ?? 0
}
} }
func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
...@@ -146,6 +164,47 @@ extension PMShowImgVideoController : UICollectionViewDelegate,UICollectionViewDa ...@@ -146,6 +164,47 @@ extension PMShowImgVideoController : UICollectionViewDelegate,UICollectionViewDa
return cell return cell
} }
} }
if state == .similarPhotos {
if collectionView == MaxCollection {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PMShowImgCellID, for: indexPath) as! PMShowImgCell
cell.icon = self.homeDataSource![indexPath.row].image!
return cell
}else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PMShowItemCellID, for: indexPath) as! PMShowItemCell
cell.icon = self.homeDataSource![indexPath.row].image!
cell.isCurrent = (self.currentIdx == indexPath.row)
return cell
}
}
if state == .similarVideos{
// 相似视频
if collectionView == MaxCollection {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PMShowVideoCellID, for: indexPath) as! PMShowVideoCell
// 获取相似视频的URL
DispatchQueue.global().async {
self.getVideoURLFromLocalIdentifier(localIdentifier: self.homeDataSource![indexPath.row].id) {[weak self] url, error in
guard self != nil else {return}
if let error = error {
print("获取视频 URL 时出错: \(error.localizedDescription)")
} else if let url = url {
DispatchQueue.main.async {
cell.homeResouceUrl = url
cell.reload()
}
}
}
}
return cell
}else{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PMShowItemCellID, for: indexPath) as! PMShowItemCell
cell.icon = self.homeDataSource![indexPath.row].image!
cell.isCurrent = (self.currentIdx == indexPath.row)
return cell
}
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "", for: indexPath) let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "", for: indexPath)
return cell return cell
} }
...@@ -157,4 +216,50 @@ extension PMShowImgVideoController : UICollectionViewDelegate,UICollectionViewDa ...@@ -157,4 +216,50 @@ extension PMShowImgVideoController : UICollectionViewDelegate,UICollectionViewDa
return CGSize(width: 68, height: 68) return CGSize(width: 68, height: 68)
} }
/// 根据LocalIdentifier获取视频的URL
/// - Parameters:
/// - localIdentifier: 资源标识
/// - completion: 回调
func getVideoURLFromLocalIdentifier(localIdentifier: String, completion: @escaping (URL?, Error?) -> Void) {
// 通过 localIdentifier 获取 PHAsset
let fetchOptions = PHFetchOptions()
let assets = PHAsset.fetchAssets(withLocalIdentifiers: [localIdentifier], options: fetchOptions)
guard let asset = assets.firstObject, asset.mediaType == .video else {
completion(nil, NSError(domain: "com.example.error", code: 1, userInfo: [NSLocalizedDescriptionKey: "未找到对应视频资源"]))
return
}
PHPhotoLibrary.requestAuthorization { status in
switch status {
case .authorized:
print("Access granted")
case .denied, .restricted:
print("Access denied")
case .notDetermined:
print("Access not determined")
@unknown default:
print("Unhandled authorization status")
}
}
let options = PHVideoRequestOptions()
options.isNetworkAccessAllowed = true // 允许从网络下载
options.deliveryMode = .automatic // 要求高质量格式
PHImageManager.default().requestAVAsset(forVideo: asset, options: options) { (avAsset, audioMix, info) in
if let error = info?[PHImageErrorKey] as? Error {
completion(nil, error)
return
}
if let urlAsset = avAsset as? AVURLAsset {
completion(urlAsset.url, nil)
} else {
completion(nil, NSError(domain: "CustomErrorDomain", code: -1, userInfo: [NSLocalizedDescriptionKey: "Failed to get video URL"]))
}
}
}
} }
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"> <plist version="1.0">
<dict> <dict>
<key>NSPhotoLibraryUsageDescription</key>
<string></string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string></string>
<key>NSAppTransportSecurity</key> <key>NSAppTransportSecurity</key>
<dict> <dict>
<key>NSAllowsArbitraryLoads</key> <key>NSAllowsArbitraryLoads</key>
......
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