gpt4 book ai didi

iOS 共享扩展未获取图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:40:00 24 4
gpt4 key购买 nike

我敢肯定,一旦有人好心地为我指出正确的方向,这就变得微不足道了,所以我很抱歉问了一个愚蠢的问题。但是我一直在寻找几天我无法弄清楚我做错了什么。

场景:创建一个接收图像文件的简单共享扩展

问题:当我访问附件时,处理程序从未被调用,尽管我可以在 itemProvider 中看到“public.jpg”但我看不到数据在哪里?

我做了什么:

1) defined NSExtensionActivationSupportsImageWithMaxCount = 1 as my only activation rule
2) added CoreMedia framework to the extension
3) added the same group to both app and app extension
4) made sure both have the group (1) in the entitlement
5) made sure both are using a certificate/app id with that group enabled
6) clean and rebuild several times to no avail.

代码:

- (void)didSelectPost {
/
for (NSExtensionItem *item in self.extensionContext.inputItems) {
for (NSItemProvider *itemProvider in item.attachments) {
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
I can hit this breakpoint --> [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(UIImage *image, NSError *error) {
but not this one --> photo = image;
}];
break;
}
}
}
.... and so on and so forth

最佳答案

您还没有发布完整的代码,但我怀疑您在错误的位置调用了 completeRequestReturningItems:completionHandler::

错误:

- (void)didSelectPost {
NSExtensionItem *item = self.extensionContext.inputItems.firstObject;
NSItemProvider *itemProvider = item.attachments.firstObject;
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage
options:nil
completionHandler:^(NSURL *url, NSError *error) {
// send the image
}];
}
// ↓ this is the wrong location ↓
[self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
}

问题是调用 completeRequestReturningItems:completionHandler: 会立即关闭 ShareViewController 并释放它。因此,包含图像的 NSItemProvider 在它可以访问图像之前也会被销毁(因为它异步加载其项目)。换句话说:您将图像发送到服务器的完成处理程序永远不会被调用,因为整个 shareViewController 已经被释放。

要解决该问题,您必须在发送图像后将对 completeRequestReturningItems:completionHandler: 的调用移动到完成处理程序的末尾。

正确:

- (void)didSelectPost {
NSExtensionItem *item = self.extensionContext.inputItems.firstObject;
NSItemProvider *itemProvider = item.attachments.firstObject;
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage
options:nil
completionHandler:^(NSURL *url, NSError *error) {
// send the image
[self.extensionContext completeRequestReturningItems:@[]
completionHandler:nil];
}];
}
}

关于iOS 共享扩展未获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32525448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com