search
LoginSignup
2

🐮インスタふうの画像選択を使いたい! YPImagePicker 🐮

使い方

Swift Package Managerで以下を加えます。

Info.Plistで Photo Usage Description と Camera Usage Description を追加します。

import YPImagePicker
var config = YPImagePickerConfiguration()
config.library.maxNumberOfItems = 10
let picker = YPImagePicker(configuration: config) 

// 以下で画像や動画を選択したときの挙動を記述

picker.didFinishPicking { [unowned picker] items, cancelled in
    for item in items {
        switch item {
            case .photo(let photo):
                let uiImage = photo.image
                print(photo)
            case .video(let video):
                let videoURL = video.url
                print(video)
            }
        }
        picker.dismiss(animated: true, completion: nil)
    }

// NavigationControllerでPresentする必要があります

self.navigationController?.present(picker, animated: true)

色々設定できます

config.isScrollToChangeModesEnabled = true
config.onlySquareImagesFromCamera = true
config.usesFrontCamera = false
config.showsPhotoFilters = true
config.showsVideoTrimmer = true
config.shouldSaveNewPicturesToAlbum = true
config.albumName = "DefaultYPImagePickerAlbumName"
config.startOnScreen = YPPickerScreen.photo
config.screens = [.library, .photo]
config.showsCrop = .none
config.targetImageSize = YPImageSize.original
config.overlayView = UIView()
config.hidesStatusBar = true
config.hidesBottomBar = false
config.hidesCancelButton = false
config.preferredStatusBarStyle = UIStatusBarStyle.default
config.bottomMenuItemSelectedColour = UIColor(r: 38, g: 38, b: 38)
config.bottomMenuItemUnSelectedColour = UIColor(r: 153, g: 153, b: 153)
config.filters = [DefaultYPFilters...]
config.maxCameraZoomFactor = 1.0
config.fonts..

config.library.options = nil
config.library.onlySquare = false
config.library.isSquareByDefault = true
config.library.minWidthForItem = nil
config.library.mediaType = YPlibraryMediaType.photo
config.library.defaultMultipleSelection = false
config.library.maxNumberOfItems = 1
config.library.minNumberOfItems = 1
config.library.numberOfItemsInRow = 4
config.library.spacingBetweenItems = 1.0
config.library.skipSelectionsGallery = false
config.library.preselectedItems = nil
config.library.preSelectItemOnMultipleSelection = true

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

PHPickerViewControllerとの違い

同じような画像動画選択システムに、AppleのモジュールであるPHPickerViewControllerがあります。
ちがいとして、PHPickerViewControllerは、複数枚選択時に画像が選択順と関係なくロードされるのに対して、YPImagePickerは選択順にロードされることがあります。

🐣


フリーランスエンジニアです。
お仕事のご相談こちらまで
rockyshikoku@gmail.com

Core MLやARKitを使ったアプリを作っています。
機械学習/AR関連の情報を発信しています。

Twitter
Medium
GitHub

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
2