我的主页上有一个按钮,点击该按钮会弹出一个子视图,显示一些可在社交媒体上分享的选项。到目前为止,我有以下代码:
- (IBAction)showActivityView:(id)sender {
NSString *shareText = @"The text I am sharing";
//UIImage how can I add an image
NSArray *itemsToShare = @[shareText];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil]; //creating mini view controller that pops up that shows the social options, subclass of view controller
activityVC.excludedActivityTypes = @[UIActivityTypeAirDrop, UIActivityTypeAssignToContact, UIActivityTypeMail, UIActivityTypePostToTencentWeibo, UIActivityTypePrint, UIActivityTypeCopyToPasteboard];
[self presentViewController:activityVC animated:YES completion:nil];
}
这个功能正如我所愿,但我想弄清楚如何添加图像(第二行)。我以为它会UIImage *shareImage = @"img.png";
但它似乎不起作用。我的另一个问题是:有没有一种方法可以让UIImage
行生效后,我是否可以设置一段代码,在共享之前截取用户的高分,而不是创建状态更新或带有静态图像的推文?
答案 0 :(得分:1)
迟到的答案,但这是我用于类似的代码:
//takes screenshot
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [keyWindow bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];
UIImage *capturedScreen = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Share image and text
NSString *text = [NSString stringWithFormat:@"My score"];
UIActivityViewController *controller =
[[UIActivityViewController alloc]
initWithActivityItems:@[text, capturedScreen]
applicationActivities:nil];
controller.excludedActivityTypes = @[
UIActivityTypeAssignToContact,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo,
];
[self presentViewController:controller animated:YES completion:nil];
我希望有所帮助。