Couple of questions regarding PHPicker

Hi, I've recently added PHPicker to my app and have gotten some returns and low sales that may or may not be a result of the change and just want to make sure my implementation is correct.

I'm concerned about the pickerDidFinishPicking function in particular. Here is my simple implementation that allows the user to select one image from their photo library:

Code Block
- (void) presentPHPickerController
{
    PHPickerConfiguration *config = [[PHPickerConfiguration alloc] init];
    config.filter = [PHPickerFilter imagesFilter];
    PHPickerViewController *pickerViewController = [[PHPickerViewController alloc] initWithConfiguration:config];
    pickerViewController.delegate = self;
    [self.navigationController presentViewController:pickerViewController animated:YES completion:nil];
}
- (void) picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPickerResult *> *)results
{
    PHPickerResult *result = [results firstObject];
    
    if (result)
    {
       [result.itemProvider loadObjectOfClass:[UIImage class] completionHandler:^(__kindof id<NSItemProviderReading>  _Nullable object, NSError * _Nullable error)
       {
             dispatch_async(dispatch_get_main_queue(),
            ^{
                [picker dismissViewControllerAnimated:YES completion:nil];
                 
                if (!error)
                {
                    /*do something with the image*/
                }
                else
                {
                    /*present alert*/
                }
            });
       }];
    }
    else
    {
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
}

My main concern is dismissing the picker on the main thread after the image has been loaded successfully. In most examples, I see people dismiss the picker at the very start of this function. I prefer to have the behavior where it gets dismissed after the user selects the photo however. Is there any issue with doing this?

My other question is, if the loadObjectOfClass executes successfully without error, is it guarenteed there will be a UIImage in "object"?

Thanks for the help.

Accepted Reply

  1. Yes, it will be an UIImage instance if you request [UIImage class].

  2. You should check canLoadObjectOfClass: before calling loadObjectOfClass.

  3. It's recommended to dismiss the picker first. Your app can download the asset in the background so it won't block user interaction.

Replies

  1. Yes, it will be an UIImage instance if you request [UIImage class].

  2. You should check canLoadObjectOfClass: before calling loadObjectOfClass.

  3. It's recommended to dismiss the picker first. Your app can download the asset in the background so it won't block user interaction.