简体   繁体   中英

Share image using share extension in ios8

Hi i am developing one social network app. In that I required to share the image using extension to my app API. I am developing my app by objective C not Swift . Can any body help me to solve this problem.

Creating a Share Extension in objective C

  1. App extension must have a containing app - you can't just create an app extension to be downloaded from the store, first create a regular app to contain the app extension. For the sake of this demonstration just create a new single view project and leave it untouched. Go to File->New->Project and select Single view application under iOS -> Applications call it 'ExtendableApp'.

  2. Go to File->New->Target and select Share Extension under iOS -> Application Extensions call it 'myShareExtension' this will add the share Extension target to your project.

  3. The extension ShareViewController inherit from SLComposeServiceViewController which already has a View with a Textbox, imageview and 'Cancel' and 'Post' buttons and some other features like character count, configuration, content validation.

    If you want to create your custom experience simply set your ShareViewController to inherit from UIViewController, Once your extension is activated all the regular viewDidLoad, viewDidAppear, etc will be called.

At this point after installing your containing app you will already by able to see 'myShareExtension' in UIActivityViewController menu

Get the shared UIImage

In your ShareViewController.mm in viewDidAppear use the following to get the image

-(void)viewDidAppear:(BOOL)animated
{
    for (NSItemProvider* itemProvider in ((NSExtensionItem*)self.extensionContext.inputItems[0]).attachments )
    {
        if([itemProvider hasItemConformingToTypeIdentifier:@"public.image"])
        {
            [itemProvider loadItemForTypeIdentifier:@"public.image" options:nil completionHandler:
                 ^(id<NSSecureCoding> item, NSError *error)
                 {
                     UIImage *sharedImage = nil;
                     if([(NSObject*)item isKindOfClass:[NSURL class]])
                     {
                         sharedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:(NSURL*)item]];
                     }
                     if([(NSObject*)item isKindOfClass:[UIImage class]])
                     {
                         sharedImage = (UIImage*)item;
                     }
                 }];
        }
    }
}

Note - This code is only for demonstration, extensions should be quick and lightweight and not block the UI thread while loading an image, in real application you would do this in the background.

Specify when the extension shows up

by default the extension will now show up whenever the UIActivityViewController menu appears, to specify in which scenarios the extension should appear you need to set the proper values in the extension info.plist under NSExtension, NSExtensionAttributes, NSExtensionActivationRule You can find a decumentation of the available keys here: Information Property List Key Reference

Note that the default behavior is for your extension to appear whenever all of the keys apply, that means that if you specify NSExtensionActivationSupportsImageWithMaxCount and NSExtensionActivationSupportsMovieWithMaxCount your extension will appear only when the user is sharing both image And movie not image or movie. To write an extension which appears for either one of a few shared data types look here

http://bryan.io/post/97658826431/what-we-learned-building-the-tumblr-ios-share-extension

Declaring Supported Data Types for a Share or Action Extension

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM