简体   繁体   English

在ios8中使用共享扩展名共享图像

[英]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. 我需要使用我的应用API扩展程序来共享图像。 I am developing my app by objective C not Swift . 我正在通过目标C而不是Swift开发我的应用程序。 Can any body help me to solve this problem. 任何人都可以帮我解决这个问题。

Creating a Share Extension in objective C 在目标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'. 转到File-> New-> Project并选择iOS下的单视图应用程序 - > Applications将其命名为'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. 转到文件 - >新建 - >目标并选择iOS下的共享扩展 - >应用程序扩展称之为'myShareExtension'这将为您的项目添加共享扩展目标。

  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. 扩展ShareViewController继承自SLComposeServiceViewController,SLComposeServiceViewController已经具有带文本框,图像视图和“取消”和“发布”按钮的视图以及一些其他功能,如字符数,配置,内容验证。

    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. 如果您想创建自定义体验,只需将ShareViewController设置为从UIViewController继承,一旦您的扩展被激活,将调用所有常规viewDidLoad,viewDidAppear等。

At this point after installing your containing app you will already by able to see 'myShareExtension' in UIActivityViewController menu 在安装包含应用程序之后,您已经可以在UIActivityViewController菜单中看到“myShareExtension”了

Get the shared UIImage 获取共享的UIImage

In your ShareViewController.mm in viewDidAppear use the following to get the image 在viewDidAppear的ShareViewController.mm中,使用以下命令获取图像

-(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. 注意 - 此代码仅用于演示,扩展应该快速且轻量级,并且在加载图像时不会阻止UI线程,在实际应用程序中,您将在后台执行此操作。

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 默认情况下,只要出现UIActivityViewController菜单,现在就会显示扩展名,以指定扩展应该出现在哪些场景中,您需要在NSExtension,NSExtensionAttributes,NSExtensionActivationRule下的扩展名info.plist中设置正确的值您可以找到可用的扩展这里的键: 信息属性列表键参考

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. 请注意,默认行为是在所有键都适用时显示扩展名,这意味着如果指定NSExtensionActivationSupportsImageWithMaxCountNSExtensionActivationSupportsMovieWithMaxCount则只有当用户共享图像电影而不是图像或电影时,才会显示扩展名。 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 http://bryan.io/post/97658826431/what-we-learned-building-the-tumblr-ios-share-extension

Declaring Supported Data Types for a Share or Action Extension 声明共享或操作扩展的支持数据类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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