YPImagePicker

repository·master·Indexed 26 days ago

https://github.com/yummypets/ypimagepicker

A feature-rich, Instagram-like photo and video picker for iOS written in pure Swift. It provides customizable interfaces for camera capture, library selection, cropping, filtering, and video trimming. Supports multiple selection, custom wordings, and is configurable via YPImagePickerConfiguration. Minimum target iOS version is 12.0.

Tokens
1.9K
Snippets
10
Records
12
Agent score
39%

What's inside YPImagePicker

  1. Configure required Plist entries

    master

    To access the camera and photo libraries, you must add the following keys to your Info.plist with appropriate usage descriptions:

    <key>NSCameraUsageDescription</key>
    <string>yourWording</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>yourWording</string>
    <key>NSMicrophoneUsageDescription</key>
    <string>yourWording</string>
  2. Set iPad preferred content size

    master

    When displaying the picker on iPad (e.g., in a popover), you must set the widthOniPad property in YPImagePickerConfiguration before presenting the picker.

    let preferredContentSize = CGSize(width: 500, height: 600)
    YPImagePickerConfiguration.widthOniPad = preferredContentSize.width
  3. Install YPImagePicker via Swift Package Manager

    master

    You can add YPImagePicker via Swift Package Manager by using the repository URL: https://github.com/Yummypets/YPImagePicker.git.

    Note: The minimum target iOS version is 12.0.

    .package(
        name: "YPImagePicker",
        url: "https://github.com/Yummypets/YPImagePicker.git",
        .upToNextMajor(from: "5.0.0")
    )
  4. Set a global default configuration

    master

    You can set a shared configuration that will be used by all YPImagePicker instances that are initialized without a specific configuration object.

    // Set the default configuration for all pickers
    YPImagePickerConfiguration.shared = config
    
    // And then use the default configuration like so:
    let picker = YPImagePicker()
  5. Configure Library settings

    master

    Use config.library to customize the behavior of the photo/video library screen. Key properties include:

    • mediaType: Set to .photo or .video.
    • maxNumberOfItems: Limits the number of items a user can select.
    • minNumberOfItems: Sets the minimum required selection.
    • onlySquare: Boolean to restrict selection to square images.
    • isSquareByDefault: Boolean to default selection to square.
    • numberOfItemsInRow: Number of items displayed per row.
  6. Configure YPImagePicker via YPImagePickerConfiguration

    master

    All settings are managed through the YPImagePickerConfiguration struct. You can create a custom configuration and pass it to the YPImagePicker initializer.

    var config = YPImagePickerConfiguration()
    // [Edit configuration here ...]
    let picker = YPImagePicker(configuration: config)
  7. Configure Video settings

    master

    Use config.video to control video recording and trimming behavior:

    • recordingTimeLimit: Maximum recording time in seconds.
    • libraryTimeLimit: Maximum video duration from library in seconds.
    • minimumTimeLimit: Minimum duration required for a video.
    • trimmerMaxDuration: Maximum duration allowed in the trimmer.
    • trimmerMinDuration: Minimum duration allowed in the trimmer.
    • fileType: The video file format (e.g., .mov).
  8. Pick a single video

    master

    To restrict the picker to videos only, configure the screens to include .video and set library.mediaType to .video.

    // Here we configure the picker to only show videos, no photos.
    var config = YPImagePickerConfiguration()
    config.screens = [.library, .video]
    config.library.mediaType = .video
    
    let picker = YPImagePicker(configuration: config)
    picker.didFinishPicking { [unowned picker] items, _ in
        if let video = items.singleVideo {
            print(video.fromCamera)
            print(video.thumbnail)
            print(video.url)
        }
        picker.dismiss(animated: true, completion: nil)
    }
    present(picker, animated: true, completion: nil)
  9. Perform multiple selection

    master

    To enable multiple selection, set library.maxNumberOfItems in your configuration. Iterate through the items in the didFinishPicking callback and switch over the enum cases .photo and .video.

    var config = YPImagePickerConfiguration()
    config.library.maxNumberOfItems = 3
    let picker = YPImagePicker(configuration: config)
    
    picker.didFinishPicking { [unowned picker] items, cancelled in
        for item in items {
            switch item {
            case .photo(let photo):
                print(photo)
            case .video(let video):
                print(video)
            }
        }
        picker.dismiss(animated: true, completion: nil)
    }
  10. Pick a single photo

    master

    To pick a single photo, initialize YPImagePicker and use the didFinishPicking callback. Use the items.singlePhoto helper to access the selected photo's properties.

    let picker = YPImagePicker()
    picker.didFinishPicking { [unowned picker] items, _ in
        if let photo = items.singlePhoto {
            print(photo.fromCamera) // Image source (camera or library)
            print(photo.image) // Final image selected by the user
            print(photo.originalImage) // original image selected by the user, unfiltered
            print(photo.modifiedImage) // Transformed image, can be nil
            print(photo.exifMeta) // Print exif meta data of original image.
        }
        picker.dismiss(animated: true, completion: nil)
    }
    present(picker, animated: true, completion: nil)