0

I have the following function which loads an image using the new PHPickerViewController:

- (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(),
            ^{
                if (!error)
                {
                    [self pickerControllerDidFinishPickingWithImage:(UIImage *)object];
                }
                else
                {
                    [self pickerControllerDidFinishWithError];
                }
            });
       }];
    }
}

I want to avoid using loadObjectOfClass because I don't want the image to load asynchronously. So I opted to use loadItemForTypeIdentifier instead and did the following:

   [result.itemProvider loadItemForTypeIdentifier:@"public.image" options:nil completionHandler:^(__kindof id<NSItemProviderReading>  _Nullable object, NSError * _Nullable error)
   {
         dispatch_async(dispatch_get_main_queue(),
        ^{
            if (!error)
            {
                [self pickerControllerDidFinishPickingWithImage:(UIImage *)object];
            }
            else
            {
                [self pickerControllerDidFinishWithError];
            }
        });
   }]; 

However, when I run this code I am getting:

ncaught exception 'NSInvalidArgumentException', reason: '-[NSURL imageOrientation]: unrecognized selector sent to instance 0x600001ce5920' terminating with uncaught exception of type NSException

I am not that great with blocks and this is my first time using a UTI so I'm wondering, what am I doing wrong here? Is it even possible to accomplish what I'm trying to accomplish, i.e. pull a UIImage out using loadItemForTypeIdentifier? Am I getting the syntax wrong?

NSMoron
  • 141
  • 1
  • 10
  • 1
    The error you get is because you receive a `NSURL` object, not `UIImage`. I don't really get your reason to use the loadItemForTypeIdentifier, you said you didn't want the image to load asynchronously but according to [documentation](https://developer.apple.com/documentation/foundation/nsitemprovider/1403900-loaditemfortypeidentifier) it does load asynchronously: _If the item provider object is able to provide data in the requested type, it does so and asynchronously executes your completionHandler block with the results. The block may be executed on a background thread._ – ScorpiCon Feb 11 '21 at 11:33
  • @ScorpiCon Yeah you're absolutely right, in the function descriptions on the NSItemProvider documentation page, it mentioned all the other functions as being asynchronous but not loadItemForTypeIdentifier that's why I assumed. But when you go to the loadItemForTypeIdentifier page, it does indeed say that. – NSMoron Feb 11 '21 at 11:37

0 Answers0