简体   繁体   English

如何修复此社交媒体视图共享(iOS)?

[英]How to fix this social media view sharing (iOS)?

I have a button on my main page that when clicked makes a subview pop up that displays a few options to share on social media. 我的主页上有一个按钮,单击该按钮会弹出一个子视图,该子视图显示了一些可以在社交媒体上共享的选项。 So far I have the following code: 到目前为止,我有以下代码:

- (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];
}

This functions as I'd like, but I'm trying to figure out how to add an image (second line). 这可以按我的意愿运行,但是我试图弄清楚如何添加图像(第二行)。 I thought it would be UIImage *shareImage = @"img.png"; 我以为是UIImage *shareImage = @"img.png"; but it didn't seem to work. 但它似乎没有用。 My other question is: Is there a way that after getting that UIImage line to work, is it possible that I could set up a piece of code that screenshots the users highscore before sharing it, as opposed to creating a status update or tweet with a static image? 我的另一个问题是:有没有一种方法可以使UIImage行正常工作,是否有可能我可以设置一段在共享之前截图用户高分的代码,而不是使用静态图片?

Late answer, but here is the code that I'm using for something similar: 较晚的答案,但这是我用于类似操作的代码:

 //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];

I hope that help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM