分享一篇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];
}
}