@ -61,6 +61,7 @@ struct QRCodeStyleView: View {
// 加 载 状 态
@ State private var isLoading = false
@ State private var showingSavedView = false
// 选 中 的 标 签 类 型
@ State private var selectedTabType : TabType = . colors
@ -142,6 +143,20 @@ struct QRCodeStyleView: View {
targetSize : CGSize ( width : 80 , height : 80 )
)
}
. background (
NavigationLink (
destination : QRCodeSavedView (
qrCodeImage : generateQRCodeImage ( ) ,
qrCodeContent : qrCodeContent ,
qrCodeType : qrCodeType ,
styleData : createStyleData ( ) ,
historyItem : historyItem
) ,
isActive : $ showingSavedView
) {
EmptyView ( )
}
)
}
// MARK: - 二 维 码 预 览 区 域
@ -572,20 +587,15 @@ struct QRCodeStyleView: View {
// MARK: - 保 存 二 维 码
private func saveQRCode ( ) {
// 生 成 二 维 码 图 片
let qrCodeImage = generateQRCodeImage ( )
// 保 存 到 相 册
UIImageWriteToSavedPhotosAlbum ( qrCodeImage , nil , nil , nil )
// 保 存 到 历 史 记 录
// 只 保 存 到 历 史 记 录 , 不 保 存 到 相 册
if historyItem != nil {
updateExistingHistory ( )
} else {
saveToHistory ( )
}
dismiss ( )
// 显 示 保 存 成 功 界 面
showingSavedView = true
}
// MARK: - 生 成 二 维 码 图 片
@ -599,29 +609,13 @@ struct QRCodeStyleView: View {
}
}
// MARK: - 保 存 到 历 史 记 录
private func saveToHistory ( ) {
// 确 保 在 主 线 程 上 执 行 C o r e D a t a 操 作
DispatchQueue . main . async {
do {
let context = self . coreDataManager . container . viewContext
let historyItem = HistoryItem ( context : context )
historyItem . id = UUID ( )
historyItem . dataType = DataType . qrcode . rawValue
historyItem . dataSource = DataSource . created . rawValue
historyItem . createdAt = Date ( )
historyItem . isFavorite = false
historyItem . qrCodeType = self . qrCodeType . rawValue
historyItem . content = self . qrCodeContent
print ( " 📝 创建历史记录项: \( self . qrCodeContent ) " )
// 保 存 二 维 码 样 式 数 据
// MARK: - 创 建 样 式 数 据
private func createStyleData ( ) -> QRCodeStyleData {
var logoIdentifier : String ? = nil
var hasCustomLogo = false
var customLogoFileName : String ? = nil
if let customLogo = self . customLogoImage {
if let customLogo = customLogoImage {
// 自 定 义 L o g o : 保 存 到 文 件 系 统
let fileName = " custom_ \( UUID ( ) . uuidString ) .png "
logoIdentifier = " custom_ \( UUID ( ) . uuidString ) "
@ -629,24 +623,45 @@ struct QRCodeStyleView: View {
customLogoFileName = fileName
// 保 存 图 片 到 文 件 系 统
self . saveCustomLogoToFile ( customLogo , fileName : fileName )
saveCustomLogoToFile ( customLogo , fileName : fileName )
print ( " 🖼️ 自定义Logo已保存到文件: \( fileName ) " )
} else if let selectedLogo = self . selectedLogo {
} else if let selectedLogo = selectedLogo {
// 预 设 L o g o
logoIdentifier = selectedLogo . rawValue
hasCustomLogo = false
print ( " 🏷️ 使用预设Logo: \( selectedLogo . rawValue ) " )
}
let styleData = QRCodeStyleData (
foregroundColor : self . selectedForegroundColor . rawValue ,
backgroundColor : self . selectedBackgroundColor . rawValue ,
dotType : self . selectedDotType . rawValue ,
eyeType : self . selectedEyeType . rawValue ,
return QRCodeStyleData (
foregroundColor : selectedForegroundColor . rawValue ,
backgroundColor : selectedBackgroundColor . rawValue ,
dotType : selectedDotType . rawValue ,
eyeType : selectedEyeType . rawValue ,
logo : logoIdentifier ,
hasCustomLogo : hasCustomLogo ,
customLogoFileName : customLogoFileName
)
}
// MARK: - 保 存 到 历 史 记 录
private func saveToHistory ( ) {
// 确 保 在 主 线 程 上 执 行 C o r e D a t a 操 作
DispatchQueue . main . async {
do {
let context = self . coreDataManager . container . viewContext
let historyItem = HistoryItem ( context : context )
historyItem . id = UUID ( )
historyItem . dataType = DataType . qrcode . rawValue
historyItem . dataSource = DataSource . created . rawValue
historyItem . createdAt = Date ( )
historyItem . isFavorite = false
historyItem . qrCodeType = self . qrCodeType . rawValue
historyItem . content = self . qrCodeContent
print ( " 📝 创建历史记录项: \( self . qrCodeContent ) " )
// 保 存 二 维 码 样 式 数 据
let styleData = self . createStyleData ( )
print ( " 🎨 样式数据创建成功: \( styleData . styleDescription ) " )
@ -699,36 +714,7 @@ struct QRCodeStyleView: View {
let context = self . coreDataManager . container . viewContext
// 保 存 二 维 码 样 式 数 据
var logoIdentifier : String ? = nil
var hasCustomLogo = false
var customLogoFileName : String ? = nil
if let customLogo = self . customLogoImage {
// 自 定 义 L o g o : 保 存 到 文 件 系 统
let fileName = " custom_ \( UUID ( ) . uuidString ) .png "
logoIdentifier = " custom_ \( UUID ( ) . uuidString ) "
hasCustomLogo = true
customLogoFileName = fileName
// 保 存 图 片 到 文 件 系 统
self . saveCustomLogoToFile ( customLogo , fileName : fileName )
print ( " 🖼️ 自定义Logo已保存到文件: \( fileName ) " )
} else if let selectedLogo = self . selectedLogo {
// 预 设 L o g o
logoIdentifier = selectedLogo . rawValue
hasCustomLogo = false
print ( " 🏷️ 使用预设Logo: \( selectedLogo . rawValue ) " )
}
let styleData = QRCodeStyleData (
foregroundColor : self . selectedForegroundColor . rawValue ,
backgroundColor : self . selectedBackgroundColor . rawValue ,
dotType : self . selectedDotType . rawValue ,
eyeType : self . selectedEyeType . rawValue ,
logo : logoIdentifier ,
hasCustomLogo : hasCustomLogo ,
customLogoFileName : customLogoFileName
)
let styleData = self . createStyleData ( )
print ( " 🎨 样式数据更新成功: \( styleData . styleDescription ) " )
@ -848,7 +834,7 @@ struct QRCodeStyleView: View {
}
// 方 法 3 : 尝 试 从 B u n d l e 的 R e s o u r c e s 目 录 加 载
if let bundlePath = Bundle . main . path ( forResource : " Resources " , ofType : nil ) {
if Bundle . main . path ( forResource : " Resources " , ofType : nil ) != nil {
for subdirectory in subdirectories {
if let imagePath = Bundle . main . path ( forResource : name , ofType : " png " , inDirectory : subdirectory ) {
return UIImage ( contentsOfFile : imagePath )
@ -1037,7 +1023,7 @@ struct ImagePicker: UIViewControllerRepresentable {
let height = cgImage . height
let bitsPerComponent = cgImage . bitsPerComponent
let bytesPerRow = cgImage . bytesPerRow
let colorSpace = cgImage . colorSpace
_ = cgImage . colorSpace
// 计 算 内 存 大 小 ( 字 节 )
let memorySizeInBytes = height * bytesPerRow