在iOS8中使用共享扩展名共享图像 [英] Share image using share extension in ios8

查看:89
本文介绍了在iOS8中使用共享扩展名共享图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个社交网络应用.为此,我需要使用我的应用程序API的扩展名共享图像.我正在通过客观C 而不是 Swift 开发我的应用.任何人都可以帮助我解决这个问题.

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.

推荐答案

在目标C中创建共享扩展

  1. 应用程序扩展名必须具有包含的应用程序-您不能仅创建要从商店下载的应用程序扩展名,首先要创建一个常规应用程序以包含该应用程序扩展名. 为了进行此演示,只需创建一个新的单视图项目,然后使其保持不变即可. 转到文件->新建->项目,然后在 iOS->下选择单视图应用程序.应用程序将其称为"ExtendableApp".

  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'.

转到文件->新建->目标,然后在 iOS->下选择 Share Extension .应用程序扩展,将其称为"myShareExtension",这会将共享扩展目标添加到您的项目中.

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.

ShareViewController扩展继承自SLComposeServiceViewController,SLComposeServiceViewController已经具有带文本框的View,imageview,"Cancel"和"Post"按钮以及一些其他功能,例如字符计数,配置,内容验证.

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设置为从UIViewController继承,一旦激活扩展,便会调用所有常规的viewDidLoad,viewDidAppear等.

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.

此时,在安装了包含应用程序之后,您已经可以在UIActivityViewController菜单中看到"myShareExtension"

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

在viewDidAppear的ShareViewController.mm中,使用以下命令获取图像

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;
                     }
                 }];
        }
    }
}

注意-此代码仅用于演示,扩展应快速且轻巧,并且在加载图像时不应阻塞UI线程,在实际应用程序中,您将在后台执行此操作.

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.

默认情况下,扩展名现在将在UIActivityViewController菜单出现时显示,以指定扩展名应在哪种情况下出现,您需要在扩展名info.plist中的NSExtension,NSExtensionAttributes,NSExtensionActivationRule下设置适当的值 您可以在此处找到可用键的变体: 信息属性列表键参考

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

请注意,扩展名的默认行为是在应用所有键时都会显示扩展名,这意味着如果您指定NSExtensionActivationSupportsImageWithMaxCountNSExtensionActivationSupportsMovieWithMaxCount,则扩展名仅在用户为共享图像和电影而不是图像或电影. 要编写针对几种共享数据类型之一显示的扩展名,请点击此处

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/我们学习了如何构建tumblr-ios共享扩展名

为共享或操作扩展声明支持的数据类型

这篇关于在iOS8中使用共享扩展名共享图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆