截图后的微信、QQ分享

先看效果图:
ShotScreenImage.jpg
文件结构:
image.png
配置:
image.png

image.png
image.png

Info.plist 中添加

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>weixin</string>
        <string>mqq</string>
        <string>mqqapi</string>
        <string>mqqwpa</string>
        <string>mqqbrowser</string>
        <string>mttbrowser</string>
        <string>mqqOpensdkSSoLogin</string>
        <string>mqqopensdkapiV2</string>
        <string>mqqopensdkapiV3</string>
        <string>mqqopensdkapiV4</string>
        <string>wtloginmqq2</string>
        <string>mqzone</string>
        <string>mqzoneopensdk</string>
        <string>mqzoneopensdkapi</string>
        <string>mqzoneopensdkapi19</string>
        <string>mqzoneopensdkapiV2</string>
        <string>mqqapiwallet</string>
        <string>mqqopensdkfriend</string>
        <string>mqqopensdkdataline</string>
        <string>mqqgamebindinggroup</string>
        <string>mqqopensdkgrouptribeshare</string>
        <string>tencentapi.qq.reqContent</string>
        <string>tencentapi.qzone.reqContent</string>
    </array>
实现代码:

AppDelegate.m

#import "AppDelegate.h"
#import "ShotBackView.h"
#import "WXApi.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface AppDelegate ()<WXApiDelegate, QQApiInterfaceDelegate>

@property (nonatomic, strong) ShotBackView * backView;

@property (nonatomic, strong) TencentOAuth * tencent;

@end

NSString * const WXAppId = @"******";
NSString * const TecentAppId = @"tencent******";

@implementation AppDelegate

- (ShotBackView *)backView {
    if (!_backView) {
        _backView = [[ShotBackView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _backView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    }
    return _backView;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [WXApi registerApp:WXAppId];
    _tencent = [[TencentOAuth alloc] initWithAppId:@"******" andDelegate:nil];
    
  // 截图后的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenShotMethod) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
    
    return YES;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    if ([url.scheme isEqualToString:WXAppId]) {  // 微信
        return [WXApi handleOpenURL:url delegate:self];
    } else if ([url.scheme isEqualToString:TecentAppId]) {
        [QQApiInterface handleOpenURL:url delegate:self];
        return [TencentOAuth HandleOpenURL:url];
    } else {
        return YES;
    }
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    if ([url.scheme isEqualToString:WXAppId]) {  // 微信
        return [WXApi handleOpenURL:url delegate:self];
    } else if ([url.scheme isEqualToString:TecentAppId]) {
        [QQApiInterface handleOpenURL:url delegate:self];
        return [TencentOAuth HandleOpenURL:url];
   } else {
        return YES;
    }
}

- (void) screenShotMethod {
    
    //获取截屏图片
    self.backView.shotImageData = [self imageDataScreenShot];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self.backView];

}

 //获取截屏图片
- (NSData *)imageDataScreenShot{
    CGSize imageSize = [UIScreen mainScreen].bounds.size;
    
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
        CGContextSaveGState(context);
        CGContextTranslateCTM(context, window.center.x, window.center.y);
        CGContextConcatCTM(context, window.transform);
        CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
        if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
            [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
        } else {
            [window.layer renderInContext:context];
        }
        CGContextRestoreGState(context);
    }
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return UIImagePNGRepresentation(image);
}

// QQ和微信的回调
- (void)onResp:(id)resp {
    [self.backView removeFromSuperview];
}

@end

ShotBackView.m

#import "ShotBackView.h"
#import "ShareButton.h"
#import "WXApi.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>


#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define kBottonBtnHeight 76
#define kBottomDeleteBtnHeight 55

typedef enum {
    ShareTypeWechatSession,   //微信好友
    ShareTypeWechatTimeline,  //微信朋友圈
    ShareTypeQQSession,  // QQ好友
    ShareTypeQZone  // QQ朋友圈
} ShareType;

@interface ShotBackView()

@property (nonatomic, strong) UIView * imageBottomView;

@property (nonatomic, strong) UIView * imageBackView;

@property (nonatomic, strong) UILabel * remindLabel;

@end

@implementation ShotBackView

- (instancetype) initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (UILabel *)remindLabel {
    if (!_remindLabel) {
        _remindLabel = [[UILabel alloc] init];
        _remindLabel.text = @"将此截图分享到微信好友或QQ好友";
        _remindLabel.textAlignment = NSTextAlignmentCenter;
        _remindLabel.font = [UIFont systemFontOfSize:16];
        _remindLabel.textColor = [UIColor grayColor];
    }
    return _remindLabel;
}

- (UIView *)imageBackView {
    if (!_imageBackView) {
        _imageBackView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _imageBackView.backgroundColor = [UIColor lightGrayColor];
    }
    return _imageBackView;
}

- (UIView *)imageBottomView {
    if (!_imageBottomView) {
        _imageBottomView = [[UIView alloc] init];
    }
    return _imageBottomView;
}

- (void) setupUI {
    
    ShareButton * WXSessionBtn = [ShareButton shareButtonWithFrame:CGRectMake(0, 10, SCREEN_WIDTH * 0.5, kBottonBtnHeight) title:@"微信好友" image:[UIImage imageNamed:@"blogo_weixin"]];
    [WXSessionBtn addTarget:self action:@selector(wxSessionClick:) forControlEvents:UIControlEventTouchUpInside];
    
    ShareButton * WXTimelineBtn = [ShareButton shareButtonWithFrame:CGRectMake(SCREEN_WIDTH * 0.5, 10, SCREEN_WIDTH * 0.5, kBottonBtnHeight) title:@"微信朋友圈" image:[UIImage imageNamed:@"blogo_pyq"]];
    [WXTimelineBtn addTarget:self action:@selector(wxTimelineClick:) forControlEvents:UIControlEventTouchUpInside];
    
    ShareButton * QQBtn = [ShareButton shareButtonWithFrame:CGRectMake(0, kBottonBtnHeight + 20, SCREEN_WIDTH * 0.5, kBottonBtnHeight) title:@"QQ好友" image:[UIImage imageNamed:@"blogo_qq"]];
    [QQBtn addTarget:self action:@selector(qqClick:) forControlEvents:UIControlEventTouchUpInside];
    
    ShareButton * QZoneBtn = [ShareButton shareButtonWithFrame:CGRectMake(SCREEN_WIDTH * 0.5, kBottonBtnHeight + 20, SCREEN_WIDTH * 0.5, kBottonBtnHeight) title:@"QQ空间" image:[UIImage imageNamed:@"blogo_qzone"]];
    [QZoneBtn addTarget:self action:@selector(qZoneClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton * closeBtn = [self buttonWithTitle:@"" andRect:CGRectMake(SCREEN_WIDTH/5,SCREEN_HEIGHT ,SCREEN_WIDTH*3/5,kBottomDeleteBtnHeight) andSel:@selector(closeBtn:)];
    
    [self addSubview:self.imageBackView];
    [self.imageBackView addSubview:self.remindLabel];
    [self.imageBackView addSubview:self.imageBottomView];
    
    CGFloat bottomViewHeight = kBottonBtnHeight * 2 + 10;
    self.imageBottomView.frame = CGRectMake(0, SCREEN_HEIGHT - bottomViewHeight - 18, SCREEN_WIDTH, bottomViewHeight);
    [self.imageBottomView addSubview:WXSessionBtn];
    [self.imageBottomView addSubview:WXTimelineBtn];
    [self.imageBottomView addSubview:QQBtn];
    [self.imageBottomView addSubview:QZoneBtn];
    [self addSubview:closeBtn];

    [UIView animateWithDuration:1.0 animations:^{
        self.imageBackView.transform = CGAffineTransformMakeScale(0.6, 0.6);
        closeBtn.transform = CGAffineTransformMakeTranslation(0, -kBottomDeleteBtnHeight - 20);
    }];
}

- (UIButton *) buttonWithTitle: (NSString *)title andRect: (CGRect)frame andSel: (SEL)selector{
    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    shareBtn.titleLabel.font = [UIFont systemFontOfSize:17.0];
    shareBtn.frame = frame;
    [shareBtn setImage:[UIImage imageNamed:@"cm2_clock_icn_delete"] forState:UIControlStateNormal];
    [shareBtn.layer setMasksToBounds:YES];

    [shareBtn setTitle:title forState:UIControlStateNormal];
    [shareBtn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    return shareBtn;
}

- (void)setShotImageData:(NSData *)shotImageData {
    _shotImageData = shotImageData;
    
    UIImage *image = [UIImage imageWithData:self.shotImageData];
    
    //显示图片
    UIImageView *imgV = [[UIImageView alloc]initWithImage:image];
    CGFloat imageViewW = SCREEN_WIDTH * 0.6;
    CGFloat imageViewH = SCREEN_HEIGHT * 0.6;
    imgV.frame = CGRectMake((SCREEN_WIDTH - imageViewW) * 0.5, ( SCREEN_HEIGHT - imageViewH)* 0.15, imageViewW, imageViewH);
    
    self.remindLabel.frame = CGRectMake(0, CGRectGetMaxY(imgV.frame) + 8, SCREEN_WIDTH, 30);
    
    [self.imageBackView addSubview:imgV];
}

/**
 微信好友分享
 */
- (void) wxSessionClick:(UIButton *)btn {
    [self wxShareWithType:ShareTypeWechatSession];
}
/**
 微信朋友圈
 */
- (void) wxTimelineClick:(UIButton *)btn {
    [self wxShareWithType:ShareTypeWechatTimeline];
}
/**
 QQ好友
 */
- (void) qqClick:(UIButton *)btn {
    [self qqShareWithType:ShareTypeQQSession];
}
/**
 QQ空间
 */
- (void) qZoneClick:(UIButton *)btn {
    [self qqShareWithType:ShareTypeQZone];
}

/**
 微信分享
 */
- (void) wxShareWithType: (ShareType) type{
    if ([WXApi isWXAppInstalled]) {
        WXMediaMessage * message = [WXMediaMessage message];
        [message setThumbImage:[UIImage imageWithData:self.shotImageData]];
        
        //创建多媒体对象(一定得创建, 不然跳转不了微信)
        WXImageObject *webObj = [WXImageObject object];
        webObj.imageData = self.shotImageData;
        message.mediaObject = webObj;
        
        SendMessageToWXReq * req = [SendMessageToWXReq new];
        req.bText = NO;
        req.scene = type == ShareTypeWechatSession ? WXSceneSession : WXSceneTimeline;
        req.message = message;
    
        [WXApi sendReq:req];
    } else {
        [self noInstallToRemind:@"您尚未安装微信" urlStr:@"itms-apps://itunes.apple.com/cn/app/%E5%BE%AE%E4%BF%A1/id414478124?mt=8"];
    }
}
/**
 QQ分享
 */
- (void) qqShareWithType: (ShareType) type {
    if ([TencentOAuth iphoneQQInstalled]) {
        if (type == ShareTypeQQSession) {   // 分享给好友
            QQApiImageObject *imgObj = [QQApiImageObject objectWithData:self.shotImageData previewImageData:self.shotImageData title:@"新家装通" description:@"QQ分享"];
            SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:imgObj];
            //将内容分享到qq
            [QQApiInterface sendReq:req];
        } else if (type == ShareTypeQZone) {   // 分享到QQ空间
            QQApiImageArrayForQZoneObject *img = [QQApiImageArrayForQZoneObject objectWithimageDataArray:@[self.shotImageData] title:@"新家装通" extMap:nil];
            img.shareDestType = ShareDestTypeQQ;
            SendMessageToQQReq* req = [SendMessageToQQReq reqWithContent:img];
            [QQApiInterface SendReqToQZone:req];
        }
    } else {
        [self noInstallToRemind:@"您尚未安装微信" urlStr:@"itms-apps://itunes.apple.com/cn/app/qq/id444934666?mt=8"];
    }
    
}

/**
 未安装时的提醒
 */
- (void) noInstallToRemind: (NSString *)message urlStr: (NSString *)urlStr {
    [self removeFromSuperview];

    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [alertController dismissViewControllerAnimated:YES completion:nil];
    }];
    UIAlertAction * confirmAction = [UIAlertAction actionWithTitle:@"前去安装" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSURL * url = [NSURL URLWithString:urlStr];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
        }
    }];
    [alertController addAction:confirmAction];
    [alertController addAction:cancelAction];
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window.rootViewController presentViewController:alertController animated:YES completion:nil];
}

- (void) closeBtn:(UIButton *)btn {
    [self removeFromSuperview];
}

@end

gitHub 地址:

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,284评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,115评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,614评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,671评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,699评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,562评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,309评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,223评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,668评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,859评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,981评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,705评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,310评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,904评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,023评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,146评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,933评论 2 355

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,138评论 25 707
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,657评论 18 139
  • 小雨点在学儿歌时,其中一句“悟空背着金箍棒,天上寻找白骨精”调动了她小脑袋里的好奇因素,对于孙悟空她略知一二,至...
    忆江飘雪阅读 403评论 0 2
  • 今天阴天,下雨。孩子要在窗边看雨。爸爸不让,怕没有大人照看,会从窗台掉下去。便说给他在电脑上看暴风雨。那可是他最爱...
    西从文安阅读 177评论 0 0
  • 一、View的绘制流程 View的绘制流程分为三部分,首先是进行Measure,也就是测量View的宽高,然后是L...
    Cooke_阅读 708评论 0 1