在SwiftUI可以實現(xiàn)瀏覽相冊圖片燃少,但是只能選一張束亏,無法多選,功能相對較少阵具,這里可以使用一個庫實現(xiàn)枪汪,當(dāng)然是UIKit庫,方法和自定義uikit控件差不多怔昨,庫名字YPImagePicker,github就有
上代碼
import SwiftUI
import YPImagePicker
struct YPImagePickerView: UIViewControllerRepresentable {
typealias UIViewControllerType = YPImagePicker
@Binding var pickeredImgs: [UIImage]
//@Binding var pickeredVideos: [YPMediaVideo]
var type: YPPickerScreen
var startType: YPPickerScreen
var limit: Int
var warnMax: String {
if YPPickerScreen.photo == self.type {
return "最多只能選擇 \(self.limit) 張照片"
} else {
return "最多只能選擇 \(self.limit) 個視頻"
}
}
func makeUIViewController(context: Context) -> YPImagePicker {
var config = YPImagePickerConfiguration()
config.isScrollToChangeModesEnabled = true
config.onlySquareImagesFromCamera = true
config.usesFrontCamera = false
config.showsPhotoFilters = true
config.showsVideoTrimmer = true
config.colors.filterBackgroundColor = .white
config.colors.photoVideoScreenBackgroundColor = .white
config.colors.bottomMenuItemBackgroundColor = .white
config.colors.libraryScreenBackgroundColor = .white
config.colors.multipleItemsSelectedCircleColor = UIColor(named: "#0bBB03")
config.shouldSaveNewPicturesToAlbum = true
config.albumName = "DefaultYPImagePickerAlbumName"
config.startOnScreen = self.startType
config.screens = [.library, self.type]
config.showsCrop = .none
config.targetImageSize = YPImageSize.original
config.overlayView = UIView()
config.hidesStatusBar = true
config.hidesBottomBar = false
config.preferredStatusBarStyle = UIStatusBarStyle.darkContent
config.maxCameraZoomFactor = 1.0
config.library.options = nil
config.library.onlySquare = false
config.library.isSquareByDefault = true
config.library.minWidthForItem = nil
config.library.mediaType = self.type == YPPickerScreen.photo ? YPlibraryMediaType.photo : YPlibraryMediaType.video
config.library.defaultMultipleSelection = false
config.library.maxNumberOfItems = self.limit
config.library.minNumberOfItems = 1
config.library.numberOfItemsInRow = 4
config.library.spacingBetweenItems = 1.0
config.library.skipSelectionsGallery = true
config.library.preselectedItems = nil
config.wordings.libraryTitle = "相冊"
config.wordings.cameraTitle = "拍照"
config.wordings.next = "選好了"
config.wordings.warningMaxItemsLimit = self.warnMax
// config.video.compression = AVAssetExportPresetHighestQuality
config.video.fileType = .mov
config.video.recordingTimeLimit = 60.0
config.video.libraryTimeLimit = 60.0
config.video.minimumTimeLimit = 3.0
config.video.trimmerMaxDuration = 60.0
config.video.trimmerMinDuration = 3.0
let picker = YPImagePicker(configuration: config)
picker.didFinishPicking { [unowned picker] items, _ in
for item in items {
switch item {
case .photo(let photo):
print(photo)
self.pickeredImgs.append(photo.modifiedImage ?? photo.originalImage)
case .video(let video):
print(video)
//self.pickeredVideos.append(video)
}
}
picker.dismiss(animated: true, completion: nil)
}
// present(picker, animated: true, completion: nil)
return picker
}
func updateUIViewController(_ uiViewController: YPImagePicker, context: Context) {
UIApplication.shared.endEditing()
}
}
上面的設(shè)置具體什么意思可以去官網(wǎng)查看代碼宿稀,這里我自定義了4個自定義參數(shù)趁舀,方便在多處使用,
分別是選中后的圖片祝沸,是數(shù)組實現(xiàn)多張圖片獲取矮烹,然后就是打開后的類型,最后一個是最多可選罩锐,建議不超過9張奉狈。
下面實現(xiàn)調(diào)用
.sheet(isPresented: $showImagePicker, content: {
YPImagePickerView(pickeredImgs: self.$image, type: YPPickerScreen.photo, startType: YPPickerScreen.photo, limit: 6)
})
這里建議使用sheet打開,不會sheet的就去看看之前的文章
這里傳入的image是UIImage
@State var image = [UIImage]()
如果獲取后要顯示出來還得把里面的值遍歷出來涩惑,推薦使用一個庫仁期,當(dāng)然你也能自己實現(xiàn)顯示,使用foreach即可便利出來,庫的名稱是WaterfallGrid,如何使用可以自行去github上看看
WaterfallGrid(self.image, id: \.self) {
Image(uiImage: $0)
.resizable()
.scaledToFit()
.frame(width: 120, height: 120)
}
.frame(width: self.width-20, height: 240, alignment: .leading)
.gridStyle(columns:3,spacing: 5,padding: EdgeInsets(top: 0, leading: 3, bottom: 0, trailing: 3))
.background(Color.white)
其實這個庫個人覺得不太喜歡跛蛋,我怎么調(diào)都不符合我的預(yù)期熬的,可能是我不會用吧,所以我自己實現(xiàn)了如何放置圖片赊级,這里就不展示了押框,記得嗶哩嗶哩上有人寫了,我改良后還挺好用的理逊。
查看圖片前提是獲取權(quán)限這點別忘了橡伞,然后就是這個庫默認(rèn)是有相機(jī)界面,可直接拍照晋被,也可加濾鏡什么的兑徘。