美文网首页
iOS 系统自带截屏分享

iOS 系统自带截屏分享

作者: 弹吉他的少年 | 来源:发表于2020-09-10 18:07 被阅读0次

分享一篇iOS系统自带截屏分享

使用方案

UIImage *image = [KJTools kj_shareWithHideBlock:^bool{
    /// 隐藏不需要截图的区域
    return YES;
}];
[KJTools kj_share:@[UIImagePNGRepresentation(image)] ViewController:weakself Success:^(BOOL success) {
    /// 分享完成处理
}];

简单粗暴,贴上代码

/// 分享截图隐藏多余部分
+ (UIImage*)kj_shareWithHideBlock:(bool(^)(void))block{
    if (block && block()) return [UIImage kj_captureScreenWindow];
    return nil;
}
/// 原生分享
+ (void)kj_share:(NSArray*)items ViewController:(UIViewController*)vc Success:(void (^)(BOOL success))block{
    if (items.count == 0) return;
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
    if (@available(iOS 11.0, *)) {
        activityVC.excludedActivityTypes = @[UIActivityTypeMessage, UIActivityTypeMail, UIActivityTypeOpenInIBooks, UIActivityTypeMarkupAsPDF];
    }else if (@available(iOS 9.0, *)){
        activityVC.excludedActivityTypes = @[UIActivityTypeMessage, UIActivityTypeMail, UIActivityTypeOpenInIBooks];
    }else{
        activityVC.excludedActivityTypes = @[UIActivityTypeMessage, UIActivityTypeMail];
    }
    UIActivityViewControllerCompletionWithItemsHandler itemsBlock = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
        if (block) block(completed);
    };
    activityVC.completionWithItemsHandler = itemsBlock;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        activityVC.popoverPresentationController.sourceView = vc.view;
        activityVC.popoverPresentationController.sourceRect = CGRectMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height, 0, 0);
        [vc presentViewController:activityVC animated:YES completion:nil];
    }else{
        [vc presentViewController:activityVC animated:YES completion:nil];
    }
}

备注:本文用到的部分函数方法和Demo,均来自三方库KJCategories,如有需要的朋友可自行pod 'KJCategories'引入即可

截屏分享介绍就到此完毕,后面有相关再补充,写文章不容易,还请点个小星星传送门

相关文章

网友评论

      本文标题:iOS 系统自带截屏分享

      本文链接:https://www.haomeiwen.com/subject/lcnpektx.html