在SwiftUI可以实现浏览相册图片,但是只能选一张,无法多选,功能相对较少,这里可以使用一个库实现,当然是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()
}
}
上面的设置具体什么意思可以去官网查看代码,这里我自定义了4个自定义参数,方便在多处使用,
分别是选中后的图片,是数组实现多张图片获取,然后就是打开后的类型,最后一个是最多可选,建议不超过9张。
下面实现调用
.sheet(isPresented: $showImagePicker, content: {
YPImagePickerView(pickeredImgs: self.$image, type: YPPickerScreen.photo, startType: YPPickerScreen.photo, limit: 6)
})
这里建议使用sheet打开,不会sheet的就去看看之前的文章
这里传入的image是UIImage
@State var image = [UIImage]()
如果获取后要显示出来还得把里面的值遍历出来,推荐使用一个库,当然你也能自己实现显示,使用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)
其实这个库个人觉得不太喜欢,我怎么调都不符合我的预期,可能是我不会用吧,所以我自己实现了如何放置图片,这里就不展示了,记得哔哩哔哩上有人写了,我改良后还挺好用的。
查看图片前提是获取权限这点别忘了,然后就是这个库默认是有相机界面,可直接拍照,也可加滤镜什么的。
网友评论