通过UIActivityViewController在其他应用程序*享图像/视频在iOS中显示两次共享项目
我试图使用UIActivityViewController
将一些图像和视频分享给其他应用程序(如FB,WhatsApp)。我有UIActivityItemProvider
的子分类,并且在调用-(id)item
方法时,我正在处理图像/视频并保存在文档目录中。然后我将文件路径返回为NSURLs
。我的问题是,我无法找到同时发送多个文件URL的方法。通过UIActivityViewController在其他应用程序*享图像/视频在iOS中显示两次共享项目
下面是我从 - (id)item方法返回url的方法;
- 作为
NSArray
的NSURL
对象。不工作。当目标应用弹出来时,它总是空的。 - 作为
NSDictionary
,其中NSURL
对象是值和键可以是任何东西。 PROBLEMATIC:目标应用程序弹出窗口显示所有项目,但TWICE!我尝试了很多字典,但是找不到解决这个问题的方法。
简单地返回NSURL
对象-(id)item
方法适用于单个文件。但是,我必须分享多个项目。数组不起作用,Dictionary正在复制共享项目。
有人可以告诉我我在做什么错吗?
更新1:
这是我如何展示UIActivityViewController。
CustomItemProvider *provider = [[CustomItemProvider alloc] initWithPlaceholderItem:[UIImage imageNamed:@"ios-59.png"]];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[provider] applicationActivities:nil];
activityViewController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError)
{
if(completed)
{
NSLog(@"Activity Completed");
}
else
{
NSLog(@"Activity Cancelled");
}
};
[self presentViewController:activityViewController animated:YES completion:^{}];
UIActivityItemProvider实现如下:这些网址是有效的,并且在这些位置有图片/视频。
@interface CustomItemProvider : UIActivityItemProvider
@end
@implementation CustomItemProvider
- (id)item
{
NSURL *url1 = [NSURL fileURLWithPath:@"file one url"];
NSURL *url2 = [NSURL fileURLWithPath:@"file two url"];
NSURL *url3 = [NSURL fileURLWithPath:@"file three url"];
return @{@"item1":url1, @"item2":url2, @"item3":url3}; //As NSDictionary. This causes 6 items to be shared; all files twice.
//return @[url1, url2, url3]; //As NSArray
}
@end
更新2:
的链接的问题是不同的。 我不想直接发送文件到UIActivityViewController作为参数initWithActivityItems:
。原因是可能有多个视频文件,这会导致内存警告和崩溃。另外,在将文件发送给目标应用程序之前(在-(id)item
方法中,我没有在这里显示),我将操纵这些文件,因此我需要UIActivityItemProvider
来处理背景中的文件。
更新: 这种方法是完全错误的,并且在某些情况下会导致意外的结果。对不起,如果这个答案误导任何人。
正确的做法是为每个要共享的项目创建一个UIActivityItemProvider对象。并返回要从相应的提供者对象共享的项目。对不起,我无法更新此答案之前。
大家好!
好吧,最后我明白了。这一直令我疯狂,我无法在任何地方找到任何适当的文档/答案。
解决方法是将字典返回为NSItemProvider
对象作为值。同样的密钥可以是任何与其他密钥不同的密钥。
如果有人遇到同样的问题,请在下面找到解决方案;
@interface CustomItemProvider : UIActivityItemProvider
{
NSCondition *_condition;
NSArray *_filesToShare;
}
@end
@implementation CustomItemProvider
- (id)item
{
_fetchInProgress = YES;
//Here start fetching/processing videos/photos in ANOTHER THREAD. Once the fetching/processing is complete, set the value of _fetchInProgress to NO.
//If you want to show progress, display progress view in MAIN THREAD here.
[_condition lock];
while(_fetchInProgress)
{
[_condition wait];
}
[_condition unlock];
//Here I am assuming that the files are processed and are saved to documents/cache directory and their file-paths are in _filesToShare array.
NSMutableDictionary *itemsDict = [NSMutableDictionary dictionaryWithCapacity:[_filesToShare count]];
for (int i = 0; i < [_filesToShare count]; i++)
{
NSURL *url = [NSURL fileURLWithPath:[_filesToShare objectAtIndex:i]];
NSItemProvider *item = [[NSItemProvider alloc] initWithContentsOfURL:url];
[itemsDict setObject:item forKey:@(i)];
}
return itemsDict;
}
@end
干杯!
与其他应用程序共享图像: 斯威夫特3代码 -
let image = UIImage(named: "yourimage")
let objectsToShare = [image]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
present(activityVC, animated: true, completion: nil)
很难没有看到你实际上在做什么猜测。失败的代码是什么样的? –
@PhillipMills,我编辑了问题并添加了方法。 – Suran
可能的重复[如何通过UIDocumentInteractionController共享多个文件?](http://*.com/questions/27655722/how-to-share-multiple-file-via-uidocumentinteractioncontroller) –