截圖后的微信、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>
實(shí)現(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)建多媒體對(duì)象(一定得創(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"];
    }
    
}

/**
 未安裝時(shí)的提醒
 */
- (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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,351評(píng)論 25 708
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,697評(píng)論 19 139
  • 小雨點(diǎn)在學(xué)兒歌時(shí),其中一句“悟空背著金箍棒,天上尋找白骨精”調(diào)動(dòng)了她小腦袋里的好奇因素,對(duì)于孫悟空她略知一二,至...
    憶江飄雪閱讀 453評(píng)論 0 2
  • 今天陰天,下雨。孩子要在窗邊看雨。爸爸不讓?zhuān)聸](méi)有大人照看,會(huì)從窗臺(tái)掉下去。便說(shuō)給他在電腦上看暴風(fēng)雨。那可是他最?lèi)?ài)...
    西從文安閱讀 204評(píng)論 0 0
  • 一、View的繪制流程 View的繪制流程分為三部分,首先是進(jìn)行Measure,也就是測(cè)量View的寬高,然后是L...
    Cooke_閱讀 778評(píng)論 0 1

友情鏈接更多精彩內(nèi)容