VPCamera3/SwiftProject/SwiftProject/Project/Controller/CCHomeController.swift

579 lines
23 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// CCHomeController.swift
// SwiftProject
//
// Created by soldoros on 2024/1/10.
//
import CoreImage
import Foundation
//import Observation
import VideoToolbox
import AVFoundation
import MetalKit
//import Cocoa
import SwiftUI
import CoreImage
import Foundation
//import Observation
import VideoToolbox
import AVKit
import CoreMedia
import LLCycleScrollView
import TZImagePickerController
class CCHomeController: BaseController, LLCycleScrollViewDelegate {
var heroAsset:AVAsset?
var mTopImgView:UIImageView?
var mBottomImgView:UIImageView?
var mTopCenterBtn:UIButton?
var cycleScrollView:LLCycleScrollView?
var mCenLab1:UILabel?
var mCenLab2:UILabel?
var mBottomView:UIView?
var mBottomBtn1:UIButton?
var mBottomBtn2:UIButton?
var mBottomBtn3:UIButton?
var mineView:CCMineView?
var mTopView:UIView?
var mBackView:UIView?
var selectedAssets:[Any] = []
var maxImages:NSInteger = 10
lazy var spaceAlbumPopView: CCSpaceAlbumFliterPopView = {
let view = CCSpaceAlbumFliterPopView.init(frame: CGRectMake(0, 0, KScreenWidth, KScreenHeight))
return view
}()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.tabBarController!.tabBar.isHidden = true;
self.navigationController?.tabBarController!.tabBar.isTranslucent = true;
self.setNavgationBarColor(color: UIColor.clear)
self.checkAirPlayStatus()
}
// func isExternalDeviceConnected() -> Bool {
// let screens = UIScreen.screens
//
// // 1
// if screens.count > 1 {
// return true
// }
//
// //
// for screen in screens {
// if !screen.isEqual(UIScreen.main) {
// return true
// }
// }
//
// //
// return false
// }
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black
self.navLine?.isHidden = true
// let isExternalDeviceConnected = isExternalDeviceConnected()
// if isExternalDeviceConnected {
// let deviceName = UIDevice.current.name
// print("\(deviceName)")
// } else {
// print("")
// }
// AirPlay
NotificationCenter.default.addObserver(self, selector: #selector(airPlayStatusDidChange(_:)), name: AVAudioSession.routeChangeNotification, object: nil)
//393*236
mTopImgView = UIImageView(frame: CGRect(x: 0, y: 0, width: SCREEN_Width, height: SCREEN_Height * 236/393))
view.addSubview(mTopImgView!)
mTopImgView?.image = UIImage.init(named: "BG_Top")
self.view.bringSubviewToFront(self.navtionBar!)
//393*240
mBottomImgView = UIImageView(frame: CGRect(x: 0, y: 0, width: SCREEN_Width, height: SCREEN_Height * 240/393))
mBottomImgView!.bottom = SCREEN_Height
view.addSubview(mBottomImgView!)
mBottomImgView?.image = UIImage.init(named: "BG_Bottom_Light")
//76*56
leftBtn1 = UIButton.init(type: UIButton.ButtonType.custom)
navtionBar?.addSubview(leftBtn1!)
leftBtn1?.tag = 10
leftBtn1?.isSelected = false
leftBtn1?.addTarget(self, action: #selector(navgationButtonClick2(sender:)), for: UIControl.Event.touchUpInside)
let img:UIImage = UIImage.init(named: "Frame_1171276035" as String)!
leftBtn1?.setImage(img, for: UIControl.State.normal)
leftBtn1?.frame = CGRect(x: 5, y: 10, width: 76, height: 56)
leftBtn1!.centerY = StatuBar_Height + NavBar_Height * 0.5
//76*56
rightBtn1 = UIButton.init(type: UIButton.ButtonType.custom)
navtionBar?.addSubview(rightBtn1!)
rightBtn1?.tag = 11
rightBtn1?.isSelected = false
rightBtn1?.addTarget(self, action: #selector(navgationButtonClick2(sender:)), for: UIControl.Event.touchUpInside)
let img2:UIImage = UIImage.init(named: "Icon_Menu" as String)!
rightBtn1?.setImage(img2, for: UIControl.State.normal)
rightBtn1?.frame = CGRect(x: 2, y: 10, width: 76, height: 56)
rightBtn1!.centerY = StatuBar_Height + NavBar_Height * 0.5
rightBtn1!.right = SCREEN_Width
//173*36
mTopCenterBtn = UIButton.init(type: UIButton.ButtonType.custom)
navtionBar?.addSubview(mTopCenterBtn!)
mTopCenterBtn!.backgroundColor = UIColor.hexStringToColor(hexString: "#060507")
mTopCenterBtn?.tag = 12
mTopCenterBtn?.isSelected = false
mTopCenterBtn?.addTarget(self, action: #selector(navgationButtonClick2(sender:)), for: UIControl.Event.touchUpInside)
mTopCenterBtn?.frame = CGRect(x: 2, y: 10, width: SCREEN_Width * 0.4, height: 36)
mTopCenterBtn!.centerY = StatuBar_Height + NavBar_Height * 0.5
mTopCenterBtn!.centerX = SCREEN_Width * 0.5
mTopCenterBtn!.clipsToBounds = true
mTopCenterBtn!.layer.cornerRadius = mTopCenterBtn!.height * 0.5
mTopCenterBtn!.layer.borderWidth = 0.5
mTopCenterBtn!.layer.borderColor = UIColor.white.cgColor
mTopCenterBtn!.setTitle("未连接VR设备", for: UIControl.State.normal)
mTopCenterBtn!.setTitleColor(UIColor.white, for: UIControl.State.normal)
mTopCenterBtn!.titleLabel?.font = UIFont.systemFont(ofSize: 12)
mTopCenterBtn!.isEnabled = false
centerView()
bottomButton()
}
//MARK: -
@objc private func airPlayStatusDidChange(_ notification: Notification) {
checkAirPlayStatus()
}
private func checkAirPlayStatus() {
print("设备连接变化")
let currentRoute = AVAudioSession.sharedInstance().currentRoute
let isAirPlayActive = currentRoute.outputs.contains { output in
return output.portType == AVAudioSession.Port.HDMI ||
output.portType == AVAudioSession.Port.airPlay
}
mTopCenterBtn!.setTitle((isAirPlayActive ? "已连接外部设备" : "未连接VR设备"), for: UIControl.State.normal)
mTopCenterBtn!.backgroundColor = isAirPlayActive ? UIColor.colorWithRGBINT(_r: 73, _g: 34, _b: 208) : UIColor.hexStringToColor(hexString: "#060507")
isAirPlayActive ? mTopCenterBtn!.setImage(UIImage.init(named: "linked_button"), for: .normal) : mTopCenterBtn!.setImage(nil,for: .normal)
isAirPlayActive ? mTopCenterBtn!.updateBtnEdgeInsets(style: .Left, space: 5):mTopCenterBtn!.updateBtnEdgeInsets(style: .Left, space: 0)
}
func centerView(){
//345*200
cycleScrollView = LLCycleScrollView()
cycleScrollView?.frame = CGRect.init(x: 24, y: Int(SafeAreaTop_Height) + 35, width: Int(SCREEN_Width) - 48, height: (Int(SCREEN_Width) - 48) * 200/345)
cycleScrollView!.delegate = self
//
cycleScrollView!.autoScroll = true
// 😄
cycleScrollView!.infiniteLoop = true
// (2)
cycleScrollView!.autoScrollTimeInterval = 3.0
//
cycleScrollView!.placeHolderImage = UIImage(named: "Banner_01")
// 使
cycleScrollView!.coverImage = UIImage(named: "Banner_01")
// =UIImageViewContentMode
cycleScrollView!.imageViewContentMode = .scaleToFill
// vertical || horizontal
cycleScrollView!.scrollDirection = .horizontal
// PageControl (.none, .system, .fill, .pill, .snake)
cycleScrollView!.customPageControlStyle = .snake
// .systemPageControltintColor
cycleScrollView!.customPageControlInActiveTintColor = UIColor.hexStringToColor(hexString: "#555555")
// .systemUIPageControl
cycleScrollView!.pageControlCurrentPageColor = UIColor.white
// .systemPageControl(8.0)
cycleScrollView!.customPageControlIndicatorPadding = 8.0
// PageControl (.left, .right .center)
cycleScrollView!.pageControlPosition = .center
cycleScrollView!.pageControlBottom = -130
// view
self.view.addSubview(cycleScrollView!)
//
cycleScrollView!.imagePaths = ["Banner_01","Banner_02","Banner_03"]
mCenLab1 = UILabel()
mCenLab1!.bounds = CGRect(x: 0, y: 0, width: SCREEN_Width - 30, height: 0)
mCenLab1!.textColor = UIColor.white
mCenLab1!.font = UIFont.boldSystemFont(ofSize: 18)
self.view.addSubview(mCenLab1!)
mCenLab1!.text = "Tips.1 拍摄空间照片以及视频"
mCenLab1!.sizeToFit()
mCenLab1!.top = cycleScrollView!.bottom + 32
mCenLab1!.centerX = SCREEN_Width * 0.5
mCenLab2 = UILabel()
mCenLab2!.numberOfLines = 3
mCenLab2!.textAlignment = NSTextAlignment.center
mCenLab2!.bounds = CGRect(x: 0, y: 0, width: SCREEN_Width - 30, height: 0)
mCenLab2!.textColor = UIColor.hexStringToColor(hexString: "#CDCDCD")
mCenLab2!.font = UIFont.systemFont(ofSize: 14)
self.view.addSubview(mCenLab2!)
mCenLab2!.text = "一键拍摄空间格式的照片以及视频,留住美好瞬间"
mCenLab2!.sizeToFit()
mCenLab2!.top = mCenLab1!.bottom + 16
mCenLab2!.centerX = SCREEN_Width * 0.5
}
//
func cycleScrollView(_ cycleScrollView: LLCycleScrollView, scrollTo index: NSInteger) {
print("当前页面索引:\(index)")
let arr1 = ["Tips.1 拍摄空间照片以及视频","Tips.2 视频转码","Tips.3 直连VR设备"]
let arr2 = ["一键拍摄空间格式的照片以及视频,留住美好瞬间",
"可以将任意格式视频转码为 VR 或 3D 格式",
"可以通过链接VR设备直接观看转码内容"]
DispatchQueue.main.async { [weak self] in
self!.mCenLab1!.width = SCREEN_Width - 30
self!.mCenLab1!.text = arr1[index]
self!.mCenLab1!.sizeToFit()
self!.mCenLab1!.top = self!.cycleScrollView!.bottom + 32
self!.mCenLab1!.centerX = SCREEN_Width * 0.5
self!.mCenLab2!.width = SCREEN_Width - 30
self!.mCenLab2!.text = arr2[index]
self!.mCenLab2!.sizeToFit()
self!.mCenLab2!.top = self!.mCenLab1!.bottom + 16
self!.mCenLab2!.centerX = SCREEN_Width * 0.5
}
}
func cycleScrollView(_ cycleScrollView: LLCycleScrollView, didSelectItemIndex index: NSInteger) {
print("用户点击了第\(index)个页面")
}
func bottomButton(){
let wd1 = (SCREEN_Width - 58)/2
let hi1 = wd1 * 80/167
let hi2 = wd1 * 172/167
//167*80
mBottomBtn1 = UIButton.init(type: UIButton.ButtonType.custom)
self.view.addSubview(mBottomBtn1!)
mBottomBtn1?.tag = 20
mBottomBtn1?.isSelected = false
mBottomBtn1?.addTarget(self, action: #selector(navgationButtonClick2(sender:)), for: UIControl.Event.touchUpInside)
mBottomBtn1?.frame = CGRect(x: 24, y: 10, width: wd1, height: hi1)
mBottomBtn1!.bottom = SCREEN_Height - SafeAreaBottom_Height - 200
mBottomBtn1!.titleLabel?.font = UIFont.systemFont(ofSize: 12)
mBottomBtn1?.setBackgroundImage(UIImage.init(named: "Card"), for: UIControl.State.normal)
mBottomBtn2 = UIButton.init(type: UIButton.ButtonType.custom)
self.view.addSubview(mBottomBtn2!)
mBottomBtn2?.tag = 21
mBottomBtn2?.isSelected = false
mBottomBtn2?.addTarget(self, action: #selector(navgationButtonClick2(sender:)), for: UIControl.Event.touchUpInside)
mBottomBtn2?.frame = CGRect(x: 24, y: 10, width: wd1, height: hi1)
mBottomBtn2!.top = mBottomBtn1!.bottom + 12
mBottomBtn2!.titleLabel?.font = UIFont.systemFont(ofSize: 12)
mBottomBtn2?.setBackgroundImage(UIImage.init(named: "Card_1"), for: UIControl.State.normal)
mBottomBtn3 = UIButton.init(type: UIButton.ButtonType.custom)
self.view.addSubview(mBottomBtn3!)
mBottomBtn3?.tag = 22
mBottomBtn3?.isSelected = false
mBottomBtn3?.addTarget(self, action: #selector(navgationButtonClick2(sender:)), for: UIControl.Event.touchUpInside)
mBottomBtn3?.frame = CGRect(x: 24, y: mBottomBtn1!.top, width: wd1, height: hi2)
mBottomBtn3!.left = mBottomBtn1!.right + 10
mBottomBtn3!.titleLabel?.font = UIFont.systemFont(ofSize: 12)
mBottomBtn3?.setBackgroundImage(UIImage.init(named: "Card_2"), for: UIControl.State.normal)
}
@objc public func navgationButtonClick2(sender:UIButton){
print("nihao")
//
if(sender.tag == 11){
addMenu()
}
if sender.tag == 20 {
let vc:CCSpatialShootController = CCSpatialShootController()
self.navigationController?.pushViewController(vc, animated: true)
}else if sender.tag == 21{
//
// gotoSpatialPlayVC()
CCBottomMenuPopView.share.show(imageNameArray: ["Icon_PopUp_Icon_Scenes","Icon_PopUp_Icon_Photo","Icon_PopUp_Icon_Folder"], textArray: ["空间相簿","照片图库","本机文件"]) { index in
if index == 0 {
self.spaceAlbum()
}else if index == 1 {
self.photoLibrary()
}
}
}else if sender.tag == 22 {
//VR
let view = CCSpaceAlbumFliterPopView.init(frame: CGRectMake(0, 0, KScreenWidth, KScreenHeight))
KWindow?.addSubview(view)
view.show()
//
view.selectedImageHandler = { [self] data,asset in
print("回调")
let vc = CCSpatialPhotoDisplayController()
vc.photoOriginalData = data
vc.imageAsset = asset
self.navigationController?.pushViewController(vc, animated: true)
}
//
view.selectedVideoHandler = { url,asset in
let vc = CCSpatialVideoDisplayController()
vc.sourceVideoURL = url
vc.videoOriginalPHAsset = asset
self.navigationController?.pushViewController(vc, animated: true)
}
}
}
func addMenu(){
mBackView = UIView(frame: self.view.bounds)
mBackView!.backgroundColor = UIColor.colorWithRGB(_r: 0, _g: 0, _b: 0, alpha: 0.01)
self.view.addSubview(mBackView!)
mTopView = UIView(frame: CGRect(x: 0, y: -StatuBar_Height, width: SCREEN_Width, height: StatuBar_Height))
mTopView?.backgroundColor = UIColor.hexStringToColor(hexString: "#2C2737")
self.view.addSubview(mTopView!)
let tap:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(tapsender(sender:)))
mBackView!.addGestureRecognizer(tap)
mineView = CCMineView(frame: CGRect.init(x: SCREEN_Width, y: StatuBar_Height, width: SCREEN_Width-108, height: SCREEN_Height - StatuBar_Height))
self.view.addSubview(mineView!)
mineView!.handle = {dic in
print(dic["name"] as Any)
if((dic["name"] as! String).elementsEqual("隐私政策")){
let vc:CCWebController = CCWebController()
vc.urlString = "http://www.baidu.com"
self.navigationController?.pushViewController(vc, animated: true)
}
self.deleteMenu()
}
addAnimation()
}
func addAnimation(){
UIView.animate(withDuration: 0.3) { [weak self] in
self!.mTopView?.top = 0
self!.mineView!.right = SCREEN_Width
self!.mBackView!.backgroundColor = UIColor.colorWithRGB(_r: 0, _g: 0, _b: 0, alpha: 0.5)
} completion: { re in
}
}
func deleteMenu(){
UIView.animate(withDuration: 0.3) { [weak self] in
self!.mTopView?.bottom = 0
self!.mineView!.left = SCREEN_Width
self!.mBackView!.backgroundColor = UIColor.colorWithRGB(_r: 0, _g: 0, _b: 0, alpha: 0.01)
} completion: {[weak self] re in
self!.mTopView?.removeFromSuperview()
self!.mTopView = nil
self!.mineView?.removeFromSuperview()
self!.mineView = nil
self!.mBackView?.removeFromSuperview()
self!.mBackView = nil
}
}
@objc public func tapsender(sender:UITapGestureRecognizer){
deleteMenu()
}
}
extension CCHomeController: TZImagePickerControllerDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate{
func spaceAlbum() {
KWindow?.addSubview(spaceAlbumPopView)
spaceAlbumPopView.show()
//
spaceAlbumPopView.selectedImageHandler = { [self] data,asset in
gotoPhotoTransformVC(data: data, url: nil,asset: asset)
}
//
spaceAlbumPopView.selectedVideoHandler = { [self] url,asset in
gotoVideoTransformVC(url: url,asset: asset)
}
}
func gotoPhotoTransformVC(data:Data?,url:URL?,asset:PHAsset?) {
let vc = VRPhotoTransformController()
vc.sourceImageData = data
vc.sourceImageURL = url
vc.imageAsset = asset
self.navigationController?.pushViewController(vc, animated: true)
}
func gotoVideoTransformVC(url:URL?,asset:PHAsset?) {
let vc = VRVideoTransformController()
vc.sourceVideoURL = url
vc.videoOriginalPHAsset = asset
self.navigationController?.pushViewController(vc, animated: true)
}
func photoLibrary() {
//
let status = AVCaptureDevice.authorizationStatus(for: .video)
if status == AVAuthorizationStatus.restricted || status == AVAuthorizationStatus.denied {
let alertC = UIAlertController(title: "Unable to use camera", message: "Please allow access to the camera in “Settings Privacy Camera” on iPhone", preferredStyle: .alert)
// let actionCancel = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction) in
// }
let actionSet = UIAlertAction(title: "Setting", style: .cancel) { (action:UIAlertAction) in
let url:URL = URL(string: UIApplication.openSettingsURLString)!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
// alertC.addAction(actionCancel)
alertC.addAction(actionSet)
self.present(alertC, animated: true, completion: nil)
}else{
//
let picker = UIImagePickerController.init()
picker.delegate = self;
picker.mediaTypes = ["public.image","public.movie"];
picker.sourceType = .photoLibrary;
self.present(picker, animated: true, completion: nil)
}
}
// MARK: - ========== UIImagePickerControllerDelegate ==========
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
print("info = \(info)")
if let mediaType = info[.mediaType] as? String {
switch mediaType {
case "public.image":
//
// let image : UIImage = info[.originalImage] as! UIImage
//url
let imageUrl : URL = info[.imageURL] as! URL
print("选择图片 Selected iamge URL: \(String(describing: imageUrl))")
//PHAsset
// let phAsset : PHAsset = info[UIImagePickerController.InfoKey.phAsset] as! PHAsset
gotoPhotoTransformVC(data: nil, url: imageUrl,asset: info[.phAsset] as? PHAsset)
break
case "public.movie":
let videoURL = info[.mediaURL] as? URL
print("选择视频 Selected video URL: \(String(describing: videoURL))")
gotoVideoTransformVC(url: videoURL,asset: info[.phAsset] as? PHAsset)
// sourceVideoURL = videoURL
// heroAsset1 = AVAsset(url: sourceVideoURL!)
// heroAsset = heroAsset1
// if(!isSpatialVideo(asset: heroAsset!)){
// showTextAlert(title: "", message: "")
// }
// play()
break
default:
print("未知的媒体类型")
break
}
}
picker.dismiss(animated: true, completion: nil)
// if self.photosArray.count >= self.maxImages {
// return SVProgressHUD.showInfo(withStatus: "Add up to \(self.maxImages) photos")
// }
// self.addPicture(image)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
// MARK: - ========== TZImagePickerControllerDelegate ==========
func imagePickerController(_ picker: TZImagePickerController!, didFinishPickingPhotos photos: [UIImage]!, sourceAssets assets: [Any]!, isSelectOriginalPhoto: Bool, infos: [[AnyHashable : Any]]!) {
// if self.photosArray.count >= self.maxImages {
// return SVProgressHUD.showInfo(withStatus: "Add up to \(self.maxImages) photos")
// }
//
// //
// if photos.count > self.maxImages - self.photosArray.count {
// let surplus = self.maxImages - self.photosArray.count
// return SVProgressHUD.showInfo(withStatus: "Up to \(surplus) more photos can be added")
// }
//
// self.addPictures(photos)
// selectedPhotos.append(contentsOf: photos)
// selectedAssets.append(contentsOf: assets)
// listTableView.reloadData()
}
}