保存图片到相册

This commit is contained in:
Fin 2025-05-23 10:40:52 +08:00
parent 625ef514fc
commit 4d5d90778d
4 changed files with 135 additions and 4 deletions

View File

@ -8,6 +8,7 @@
import IQKeyboardManagerSwift import IQKeyboardManagerSwift
import IQKeyboardToolbarManager import IQKeyboardToolbarManager
import SVProgressHUD
import SwiftyStoreKit import SwiftyStoreKit
import UIKit import UIKit
import UserNotifications import UserNotifications
@ -33,6 +34,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
// Realm() // Realm()
setupRealm() setupRealm()
SVProgressHUD.setMinimumDismissTimeInterval(1.5)
IQKeyboardManager.shared.isEnabled = true IQKeyboardManager.shared.isEnabled = true
IQKeyboardToolbarManager.shared.isEnabled = true IQKeyboardToolbarManager.shared.isEnabled = true
if #available(iOS 14, *), UIDevice.current.userInterfaceIdiom == .pad { if #available(iOS 14, *), UIDevice.current.userInterfaceIdiom == .pad {

View File

@ -2,6 +2,8 @@
<!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>NSPhotoLibraryAddUsageDescription</key>
<string>Save the image to the photo library</string>
<key>CFBundleURLTypes</key> <key>CFBundleURLTypes</key>
<array> <array>
<dict> <dict>

View File

@ -2668,6 +2668,35 @@
} }
} }
}, },
"noPermission" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "No permission"
}
},
"ja" : {
"stringUnit" : {
"state" : "translated",
"value" : "権限がありません"
}
},
"tr" : {
"stringUnit" : {
"state" : "translated",
"value" : "İzin yok"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "没有权限"
}
}
}
},
"Notice1" : { "Notice1" : {
"extractionState" : "manual", "extractionState" : "manual",
"localizations" : { "localizations" : {
@ -3393,6 +3422,64 @@
} }
} }
}, },
"save" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Save"
}
},
"ja" : {
"stringUnit" : {
"state" : "translated",
"value" : "保存"
}
},
"tr" : {
"stringUnit" : {
"state" : "translated",
"value" : "Kaydet"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "保存"
}
}
}
},
"saveSuccess" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Saved to your photos!"
}
},
"ja" : {
"stringUnit" : {
"state" : "translated",
"value" : "アルバムに保存したよ!"
}
},
"tr" : {
"stringUnit" : {
"state" : "translated",
"value" : "Albüme kaydedildi!"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "已保存到相册!"
}
}
}
},
"SecureConnection" : { "SecureConnection" : {
"extractionState" : "manual", "extractionState" : "manual",
"localizations" : { "localizations" : {

View File

@ -8,6 +8,8 @@
import ImageViewer_swift import ImageViewer_swift
import Kingfisher import Kingfisher
import Photos
import SVProgressHUD
import UIKit import UIKit
class MessageItemView: UIView { class MessageItemView: UIView {
@ -168,13 +170,25 @@ extension MessageItemView {
imageView.isHidden = false imageView.isHidden = false
// loadDiskFileSynchronously // loadDiskFileSynchronously
imageView.kf.setImage(with: URL(string: image), options: [.targetCache(imageCache), .keepCurrentImageWhileLoading, .loadDiskFileSynchronously]) { [weak self] result in imageView.kf.setImage(with: URL(string: image), options: [.targetCache(imageCache), .keepCurrentImageWhileLoading, .loadDiskFileSynchronously]) { [weak self] result in
//
let isDarkMode = UIScreen.main.traitCollection.userInterfaceStyle == .dark
self?.imageView.setupImageViewer(options: [.closeIcon(UIImage(named: "back")!), .theme(isDarkMode ? .dark : .light)])
guard let self else { return } guard let self else { return }
guard let image = try? result.get().image else { guard let image = try? result.get().image else {
return return
} }
//
let isDarkMode = UIScreen.main.traitCollection.userInterfaceStyle == .dark
var options: [ImageViewerOption] = [
.closeIcon(UIImage(named: "back")!),
.theme(isDarkMode ? .dark : .light)
]
if #available(iOS 14.0, *) {
options.append(.rightNavItemTitle(NSLocalizedString("save"), onTap: { _ in
// image
self.saveImageToAlbum(image)
}))
}
self.imageView.setupImageViewer(options: options)
layoutImageView(image: image) layoutImageView(image: image)
} }
} else { } else {
@ -199,3 +213,28 @@ extension MessageItemView {
} }
} }
} }
@available(iOS 14.0, *)
extension MessageItemView {
func saveImageToAlbum(_ image: UIImage) {
PHPhotoLibrary.requestAuthorization(for: .addOnly) { status in
guard status == .authorized || status == .limited else {
DispatchQueue.main.async {
SVProgressHUD.showInfo(withStatus: NSLocalizedString("noPermission"))
}
return
}
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAsset(from: image)
}) { success, error in
DispatchQueue.main.async {
if success {
SVProgressHUD.showSuccess(withStatus: NSLocalizedString("saveSuccess"))
} else {
SVProgressHUD.showError(withStatus: error?.localizedDescription)
}
}
}
}
}
}