Uploading multiple images from Share Extension

WagglyWonga

I currently have a share extension set up that will upload an image selected from the Photo app to a server. This works fine using the code below.

 int fileNum=10;

NSItemProvider *attachment = inputItem.attachments[0];
if ([attachment hasItemConformingToTypeIdentifier:(NSString*)kUTTypeImage])
{
    [attachment loadItemForTypeIdentifier:(NSString*)kUTTypeImage options:nil completionHandler:^(id item,NSError *error)
     {
         if (item)
         {
             NSLog (@"image %@",item);
             //upload image here
             NSData *data=[NSData dataWithContentsOfURL:item];
             activityRecord.activityType=@"Images";
             AppRecord *appRecord=[[AppRecord alloc] init];
             appRecord.fileName=[NSString stringWithFormat:@"activity_%@%i(%i).jpg",activityRecord.supplierID,activityRecord.activityID,fileNum];
             appRecord.fileBytes=data;
             [fileRecords addObject:appRecord];
             activityRecord.activityFiles=fileRecords;
             [[Settings getInstance] uploadActivityRecord:activityRecord withDelegate:self];
             [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
         }
     }];
}

I had a previous problem where the loadItemForTypeIdentifier method wasn't being called, and it was resolved by calling completeRequestReturningItems within the completion block. The problem I have now is that if I want to upload multiple files then I need to call loadItemForTypeIdentifier within a for loop (for each image) but how can I do that if the completeRequestReturningItems method will be called after the first image/item?

Many Thanks Paul

elsamp

I ran into the same problem recently and was able to resolve it by adding a counter and counting down as the images successfully completed their block. Within the loadItemForTypeIdentifier completion block I then check to see if all items have been called before calling the completeRequestReturningItems within a dispatch_once block (just for safety's sake).

__block NSInteger imageCount;
static dispatch_once_t oncePredicate;

NSItemProvider *attachment = inputItem.attachments[0];

if ([attachment hasItemConformingToTypeIdentifier:(NSString*)kUTTypeImage])
{
 [attachment loadItemForTypeIdentifier:(NSString*)kUTTypeImage options:nil completionHandler:^(NSData *item ,NSError *error)
 {
     if (item)
     {
         // do whatever you need to

         imageCount --;

         if(imageCount == 0){

             dispatch_once(&oncePredicate, ^{

                 [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];

             });
         }
     }
  }];
}

I can't say I feel like this is an overly elegant solution however, so if someone knows of a more appropriate way of handling this common use case I'd love to hear about it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Multiple images rename & uploading in PHP

From Dev

Screenshot Safari from Share Extension

From Dev

Share Extension not uploading full size images

From Dev

iOS8 extension : share images between container and extension

From Dev

Share multiple images on Instagram in iOS

From Dev

Uploading multiple images in codeigniter?

From Dev

Why do larger images not appear in a share extension, or action extension?

From Dev

Uploading Multiple Images using CarrierWave

From Dev

All images not uploading in multiple images upload

From Dev

Uploading multiple images with volley?

From Dev

Uploading multiple images with other parameters in Swift

From Dev

Django REST: Uploading and serializing multiple images

From Dev

Share multiple images with UIActivityViewController

From Dev

Uploading multiple images to mysql database on Apache server

From Dev

PHP: Uploading multiple images to imgur at once

From Dev

multiple images uploading and converting them to thumbnails in cakephp

From Dev

prevent facebook from uploading images with chrome extension

From Dev

UIActivityViewController to share multiple images

From Dev

Uploading multiple images to server. If only one image is bigger than size limit or wrong file extension then all other fails

From Dev

Multiple images rename & uploading in PHP

From Dev

Uploading images from Wordpress frontend

From Dev

PHP: Uploading multiple images at the same time

From Dev

Is there anyway of uploading images from the url?

From Dev

Uploading multiple images in webservice using PHP

From Dev

Uploading multiple images with paperclip?

From Dev

Uploading multiple images with Django

From Dev

Uploading multiple images in codeigniter?

From Dev

Best practice for storing and uploading multiple images from ImagePicker (iOS)

From Dev

Django multiple images not uploading

Related Related

  1. 1

    Multiple images rename & uploading in PHP

  2. 2

    Screenshot Safari from Share Extension

  3. 3

    Share Extension not uploading full size images

  4. 4

    iOS8 extension : share images between container and extension

  5. 5

    Share multiple images on Instagram in iOS

  6. 6

    Uploading multiple images in codeigniter?

  7. 7

    Why do larger images not appear in a share extension, or action extension?

  8. 8

    Uploading Multiple Images using CarrierWave

  9. 9

    All images not uploading in multiple images upload

  10. 10

    Uploading multiple images with volley?

  11. 11

    Uploading multiple images with other parameters in Swift

  12. 12

    Django REST: Uploading and serializing multiple images

  13. 13

    Share multiple images with UIActivityViewController

  14. 14

    Uploading multiple images to mysql database on Apache server

  15. 15

    PHP: Uploading multiple images to imgur at once

  16. 16

    multiple images uploading and converting them to thumbnails in cakephp

  17. 17

    prevent facebook from uploading images with chrome extension

  18. 18

    UIActivityViewController to share multiple images

  19. 19

    Uploading multiple images to server. If only one image is bigger than size limit or wrong file extension then all other fails

  20. 20

    Multiple images rename & uploading in PHP

  21. 21

    Uploading images from Wordpress frontend

  22. 22

    PHP: Uploading multiple images at the same time

  23. 23

    Is there anyway of uploading images from the url?

  24. 24

    Uploading multiple images in webservice using PHP

  25. 25

    Uploading multiple images with paperclip?

  26. 26

    Uploading multiple images with Django

  27. 27

    Uploading multiple images in codeigniter?

  28. 28

    Best practice for storing and uploading multiple images from ImagePicker (iOS)

  29. 29

    Django multiple images not uploading

HotTag

Archive