我在我的应用上使用app扩展分享
工作正常,可以回复网址和"消息"为了分享, 但是我的图像在记录时显示为零
@interface ShareViewController ()
@property (nonatomic, strong)__block NSString *urlString;
@property (nonatomic, strong)__block UIImage *photo;
@property (nonatomic, strong)NSString *msg;
@end
@implementation ShareViewController
- (BOOL)isContentValid {
// Do validation of contentText and/or NSExtensionContext attachments here
return YES;
}
- (void)viewDidLoad{
[super viewDidLoad];
//Get msg
NSExtensionContext *myExtensionContext = [self extensionContext];
NSArray *inputItems = [myExtensionContext inputItems];
for(NSExtensionItem* item in inputItems){
self.msg = [NSString stringWithFormat:@"%@", [item.attributedContentText string]];
}
// get url
NSExtensionItem *item = self.extensionContext.inputItems.firstObject;
NSItemProvider *itemProvider = item.attachments.firstObject;
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeURL]) {
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeURL options:nil completionHandler:^(NSURL *url, NSError *error) {
self.urlString = [NSString stringWithFormat:@"%@",url.absoluteString];
}];
}
//img
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(UIImage *image, NSError *error) {
self.photo = image;
}];
}
}
- (void)didSelectPost {
// This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
NSLog(@"big fat fella");
NSLog(@"msg: %@", self.msg);
NSLog(@"url %@:", self.urlString);
NSLog(@"im %@:", self.photo);
// Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
[self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
}
所以请注意我在didSelectPost上获取网址和消息,但图片显示为空,
如何获取图片? THX!
答案 0 :(得分:4)
要在Safari上共享时在共享扩展程序上自动生成图像,请使用 loadPreviewImageWithOptions:completionHandler:previewImageHandler。
[itemProvider loadPreviewImageWithOptions:nil completionHandler:^(UIImage *image, NSError *error){
if(image){
//do anything here with the image
}
}
我能够在Safari上共享时检索自动生成的缩略图,但我无法使用以下方式更改图像的大小:
NSString * const NSItemProviderPreferredImageSizeKey;
参考:https://developer.apple.com/documentation/foundation/nsitemprovider