截圖后的微信、QQ分享

先看效果圖:
ShotScreenImage.jpg
文件結(jié)構(gòu):
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>
實現(xiàn)代碼:

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和微信的回調(diào)
- (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]];
        
        //創(chuàng)建多媒體對象(一定得創(chuàng)建, 不然跳轉(zhuǎn)不了微信)
        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];
            //將內(nèi)容分享到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 地址:

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末处窥,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子雇卷,更是在濱河造成了極大的恐慌鳞陨,老刑警劉巖盛嘿,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件洛巢,死亡現(xiàn)場離奇詭異,居然都是意外死亡次兆,警方通過查閱死者的電腦和手機稿茉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人漓库,你說我怎么就攤上這事恃慧。” “怎么了渺蒿?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵痢士,是天一觀的道長。 經(jīng)常有香客問我茂装,道長怠蹂,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任少态,我火速辦了婚禮城侧,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘彼妻。我一直安慰自己嫌佑,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布侨歉。 她就那樣靜靜地躺著屋摇,像睡著了一般。 火紅的嫁衣襯著肌膚如雪为肮。 梳的紋絲不亂的頭發(fā)上摊册,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天,我揣著相機與錄音颊艳,去河邊找鬼茅特。 笑死,一個胖子當(dāng)著我的面吹牛棋枕,可吹牛的內(nèi)容都是我干的白修。 我是一名探鬼主播,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼重斑,長吁一口氣:“原來是場噩夢啊……” “哼兵睛!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起窥浪,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤祖很,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后漾脂,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體假颇,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年骨稿,在試婚紗的時候發(fā)現(xiàn)自己被綠了笨鸡。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片姜钳。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖形耗,靈堂內(nèi)的尸體忽然破棺而出哥桥,到底是詐尸還是另有隱情,我是刑警寧澤激涤,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布拟糕,位于F島的核電站,受9級特大地震影響昔期,放射性物質(zhì)發(fā)生泄漏已卸。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一硼一、第九天 我趴在偏房一處隱蔽的房頂上張望累澡。 院中可真熱鬧,春花似錦般贼、人聲如沸愧哟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蕊梧。三九已至,卻和暖如春腮介,著一層夾襖步出監(jiān)牢的瞬間肥矢,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工叠洗, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留甘改,地道東北人。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓灭抑,卻偏偏與公主長得像十艾,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子腾节,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,129評論 25 707
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理忘嫉,服務(wù)發(fā)現(xiàn),斷路器案腺,智...
    卡卡羅2017閱讀 134,656評論 18 139
  • 小雨點在學(xué)兒歌時庆冕,其中一句“悟空背著金箍棒,天上尋找白骨精”調(diào)動了她小腦袋里的好奇因素劈榨,對于孫悟空她略知一二愧杯,至...
    憶江飄雪閱讀 403評論 0 2
  • 今天陰天,下雨鞋既。孩子要在窗邊看雨力九。爸爸不讓,怕沒有大人照看邑闺,會從窗臺掉下去跌前。便說給他在電腦上看暴風(fēng)雨。那可是他最愛...
    西從文安閱讀 177評論 0 0
  • 一陡舅、View的繪制流程 View的繪制流程分為三部分抵乓,首先是進(jìn)行Measure,也就是測量View的寬高,然后是L...
    Cooke_閱讀 708評論 0 1