iOS共享扩展程序问题

iOS共享扩展程序问题

本文介绍了共享照片库中的图像时,iOS共享扩展程序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我用于共享"ShareViewController.m"中的图像的代码.

Below is a code that I use to share images within my "ShareViewController.m".

NSExtensionItem *item = [self.extensionContext.inputItems objectAtIndex:i];
NSItemProvider *itemProvider = item.attachments.firstObject;

if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeURL]) {
     [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeURL options:nil completionHandler:^(NSData *data, NSError *error) {

           NSLog(@"%@", data);

           // the rest of uploading script goes here

     }];
}

如果我从WhatsApp共享图像,一切都很好. 但是如果我要共享照片库或Facebook Messenger中的图像,则无法使用.

It all works fine if I share an image from WhatsApp. But it doesn't work if I want to share an image from Photo Library or from Facebook Messenger.

有人知道这个问题可能是什么吗?

Does anyone know what the problem might be?

谢谢

推荐答案

这是我解决的方法.我摆脱了(NSString *)kUTTypeURL]并添加了itemProvider.registeredTypeIdentifiers以获取具有所有可用类型标识符的数组.然后,我仅使用第一个可用的registeredTypeIdentifiers.firstObject.

Here is how I solved it. I got rid of (NSString *)kUTTypeURL] and added itemProvider.registeredTypeIdentifiers to get array with all the available type identifiers. Then I'm just using the first one available as registeredTypeIdentifiers.firstObject.

非常重要NSData *data更改为id<NSSecureCoding> item,这使得从中获取NSData有所不同.这一点特别重要,当从Messenger共享图像时-它们具有类型标识符"public.image",而不是像照片库或WhatsApp中的"public.jpeg""public.url".

Also, very important, NSData *data got changed to id<NSSecureCoding> item which makes it a bit different to get the NSData from it. That's important especially when sharing images from Messenger - they have type identifier "public.image" rather than "public.jpeg" or "public.url" like in Photos library or WhatsApp.

NSExtensionItem *item = [self.extensionContext.inputItems objectAtIndex:i];
NSItemProvider *itemProvider = item.attachments.firstObject;

// get type of file extention (jpeg, file, url, png ...)
NSArray *registeredTypeIdentifiers = itemProvider.registeredTypeIdentifiers;

if ([itemProvider hasItemConformingToTypeIdentifier:registeredTypeIdentifiers.firstObject) {
   [itemProvider loadItemForTypeIdentifier:registeredTypeIdentifiers.firstObject options:nil completionHandler:^(id<NSSecureCoding> item, NSError *error) {

           NSData *imgData;
           if([(NSObject*)item isKindOfClass:[NSURL class]]) {
                imgData = [NSData dataWithContentsOfURL:(NSURL*)item];
           }
           if([(NSObject*)item isKindOfClass:[UIImage class]]) {
                imgData = UIImagePNGRepresentation((UIImage*)item);
           }



          // the rest of uploading script goes here

   }];
}

这篇关于共享照片库中的图像时,iOS共享扩展程序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:38