2.9 通过社交平台分享图片

@ [toc]

2.9 通过社交平台分享图片

如果LightWork成功保存图片到图库,我们要向用户展示一个包含分享菜单的弹窗.以下代码将检查程序对几种社交平台是否提供支持,并显示弹框,该弹框会每个支持的社交平台创建一个操作按钮.这些操作按钮虽然分享到不同的平台,但是他们的创建方式很相似,所以,我们用一个帮助方法shareImageActionWithTitle:serviceType:image:来创建这些行为按钮.我们还提供一个Do not share的操作按钮,该按钮不进行分享,直接停止程序的繁忙模式:


- (void)showSaveImageSuccessAlertWithImage:(UIImage *)image {
    
    // Create a "Saved image" alert.
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Saved image" message:@"The image has been added to your Photos library. Would you like to share it with your friends?" preferredStyle:UIAlertControllerStyleAlert];
    
    // If the user has a Facebook account on this device, add a
    // "Post on Facebook" button to the alert.
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
        UIAlertAction* facebookAction = [self shareImageActionWithTitle:@"Post on Facebook" serviceType:SLServiceTypeFacebook image:image];
        [alert addAction:facebookAction];
        
    }
    // If the user has a Twitter account on this device, add a
    // "Tweet" button to the alert.
    
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
        UIAlertAction* twitterAction = [self shareImageActionWithTitle:@"Tweet" serviceType:SLServiceTypeTwitter image:image];
        [alert addAction:twitterAction];
    }
    // If the user has a Sina Weibo account on this device, add a
    // "Post on Sina Weibo" button to the alert.
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
        UIAlertAction* sinaWeiboAction = [self shareImageActionWithTitle:@"Post on Sina Weibo" serviceType:SLServiceTypeSinaWeibo image:image];
        [alert addAction:sinaWeiboAction];
        
    }
    
    // If the user has a Tencent Weibo account on this device, add a
    // "Post on Tencent Weibo" button to the alert.
    
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTencentWeibo]) {
        UIAlertAction* tencentWeiboAction = [self shareImageActionWithTitle:@"Post on Tencent Weibo" serviceType:SLServiceTypeTencentWeibo image:image];
        [alert addAction:tencentWeiboAction];
    }
    
    // Add a "Do not share" button to the alert.
    UIAlertAction* doNotShareAction = [UIAlertAction actionWithTitle:@"Do not share" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self stopBusyMode];
    }];
    [alert addAction:doNotShareAction];
    
    // Show the alert.
    [self presentViewController:alert animated:YES completion:nil];
}

当用户点击了分享按钮,会呈现一个iOS标准组合对话框,该组合框会连接到指定的社交平台.我们还会吧图像添加到组合框中.当用户取消该组合框时,我们要确保LightWork停止繁忙模式.下面是构建操作按钮的相关帮助方法:

- (UIAlertAction *)shareImageActionWithTitle:(NSString *)title serviceType:(NSString *)serviceType image:(UIImage *)image {
    UIAlertAction* action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:serviceType];
        [composeViewController addImage:image];
        [self presentViewController:composeViewController animated:YES completion:^{
            [self stopBusyMode];
        }];
    }];
    return action;
}

###返回到第二章目录###
###返回到书籍目录###

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值