Separate Photo Library and Camera Actions

It’s possible to add two separate buttons for adding image annotations from the Photo Library and Camera. The most flexible way to achieve this is to present UIImagePickerController or PHPickerViewController from your own code and then add a StampAnnotation with the image you get from the picker.

Alternatively, you can achieve this with PSPDFKit with subclasses of PSPDFKit’s ImagePickerController that only allow one source type. For example:

class OnlyPhotoLibraryPickerController: ImagePickerController {
    override class func availableImagePickerSourceTypes() -> [NSNumber] {
        return [NSNumber(value: UIImagePickerController.SourceType.photoLibrary.rawValue)]
    }
}

In a button action method in a subclass of PDFViewController, register the subclass just before showing the image picker:

@objc func togglePhotoLibrary(_ sender: UIBarButtonItem) {
    self.updateConfiguration {
        $0.overrideClass(ImagePickerController.self, with: OnlyPhotoLibraryPickerController.self)
    }
    self.annotationStateManager.toggleImagePickerController(sender, presentationOptions: nil)
}

Here’s a complete subclass of PDFViewController that adds two separate buttons — one to import images from the Photo Library, and one to import from the Camera:

class SeparateImagePickersPDFViewController: PDFViewController {
    override func commonInit(with document: Document?, configuration: PDFConfiguration) {
        super.commonInit(with: document, configuration: configuration)

        var leftBarButtonItems = navigationItem.leftBarButtonItems(for: .document) ?? []
        leftBarButtonItems += [
            UIBarButtonItem(title: "Library", style: .plain, target: self, action: #selector(togglePhotoLibrary)),
            UIBarButtonItem(title: "Camera", style: .plain, target: self, action: #selector(toggleCamera)),
        ]
        navigationItem.setLeftBarButtonItems(leftBarButtonItems, for: .document, animated: false)
        navigationItem.leftItemsSupplementBackButton = true
    }

    @objc private func togglePhotoLibrary(_ sender: UIBarButtonItem) {
        updateConfiguration {
            $0.overrideClass(ImagePickerController.self, with: OnlyPhotoLibraryPickerController.self)
        }
        annotationStateManager.toggleImagePickerController(sender, presentationOptions: nil)
    }

    @objc private func toggleCamera(_ sender: UIBarButtonItem) {
        updateConfiguration {
            $0.overrideClass(ImagePickerController.self, with: OnlyCameraPickerController.self)
        }
        annotationStateManager.toggleImagePickerController(sender, presentationOptions: nil)
    }
}

private class OnlyPhotoLibraryPickerController: ImagePickerController {
    override class func availableImagePickerSourceTypes() -> [NSNumber] {
        return [NSNumber(value: UIImagePickerController.SourceType.photoLibrary.rawValue)]
    }
}

private class OnlyCameraPickerController: ImagePickerController {
    override class func availableImagePickerSourceTypes() -> [NSNumber] {
        return [NSNumber(value: UIImagePickerController.SourceType.camera.rawValue)]
    }
}
@interface SeparateImagePickersPDFViewController : PSPDFViewController
@end
@interface OnlyPhotoLibraryPickerController : PSPDFImagePickerController
@end
@interface OnlyCameraPickerController : PSPDFImagePickerController
@end

@implementation SeparateImagePickersPDFViewController

- (void)commonInitWithDocument:(PSPDFDocument *)document configuration:(PSPDFConfiguration *)configuration {
    [super commonInitWithDocument:document configuration:configuration];

    NSMutableArray<UIBarButtonItem *> *leftBarButtonItems = [NSMutableArray arrayWithArray:[self.navigationItem leftBarButtonItemsForViewMode:PSPDFViewModeDocument]];
    [leftBarButtonItems addObjectsFromArray:@[
        [[UIBarButtonItem alloc] initWithTitle:@"Library" style:UIBarButtonItemStylePlain target:self action:@selector(togglePhotoLibrary:)],
        [[UIBarButtonItem alloc] initWithTitle:@"Camera" style:UIBarButtonItemStylePlain target:self action:@selector(toggleCamera:)],
    ]];
    [self.navigationItem setLeftBarButtonItems:leftBarButtonItems forViewMode:PSPDFViewModeDocument animated:NO];
    self.navigationItem.leftItemsSupplementBackButton = YES;
}

- (void)togglePhotoLibrary:(UIBarButtonItem *)sender {
    [self updateConfigurationWithBuilder:^(PSPDFConfigurationBuilder *builder) {
        [builder overrideClass:PSPDFImagePickerController.class withClass:OnlyPhotoLibraryPickerController.class];
    }];
    [self.annotationStateManager toggleImagePickerController:sender presentationOptions:nil];
}

- (void)toggleCamera:(UIBarButtonItem *)sender {
    [self updateConfigurationWithBuilder:^(PSPDFConfigurationBuilder *builder) {
        [builder overrideClass:PSPDFImagePickerController.class withClass:OnlyCameraPickerController.class];
    }];
    [self.annotationStateManager toggleImagePickerController:sender presentationOptions:nil];
}

@end

@implementation OnlyPhotoLibraryPickerController

+ (NSArray<NSNumber *> *)availableImagePickerSourceTypes {
    return @[@(UIImagePickerControllerSourceTypePhotoLibrary)];
}

@end

@implementation OnlyCameraPickerController

+ (NSArray<NSNumber *> *)availableImagePickerSourceTypes {
    return @[@(UIImagePickerControllerSourceTypeCamera)];
}

@end