cocos与微信分享

javascript]  view plain  copy
  1. // 微信分享屏幕截图  
  2.     shareScreenshot: function () {  
  3.         // 网页端不支持  
  4.         if (cc.sys.isBrowser) {  
  5.             cc.log('网页端不支持微信分享~');  
  6.             return;  
  7.         }  
  8.   
  9.         // 正在截图中判断  
  10.         if (this._isCapturing) {  
  11.             return;  
  12.         }  
  13.         this._isCapturing = true;  
  14.   
  15.         // 截图文件判断  
  16.         var fileName = "shareScreenshot.jpg";  
  17.         var fullPath = jsb.fileUtils.getWritablePath() + fileName;  
  18.         if (jsb.fileUtils.isFileExist(fullPath)) {  
  19.             jsb.fileUtils.removeFile(fullPath);  
  20.         }  
  21.   
  22.         // 截图并保存图片文件  
  23.         var size = cc.director.getWinSize(); // 获取视图大小  
  24.         var texture = new cc.RenderTexture(size.width, size.height); // 新建渲染纹理  
  25.         texture.setPosition(cc.p(size.width / 2, size.height / 2)); // 移动渲染纹理到视图中心  
  26.         texture.begin(); // 开始渲染  
  27.         cc.director.getRunningScene().visit(); // 访问当前场景  
  28.         texture.end(); // 渲染结束  
  29.         texture.saveToFile(fileName, cc.IMAGE_FORMAT_JPG); // 保存渲染纹理到图片文件  
  30.         console.log("渲染纹理完成");  
  31.         // 延时50毫秒,检测截图文件是否存在  
  32.         // 重复10次这个过程,如果超过10次仍没检测到图片文件,截图失败(超时)  
  33.         var self = this;  
  34.         var tryTimes = 0;  
  35.         var fn = function () {  
  36.             if (jsb.fileUtils.isFileExist(fullPath)) {  
  37.                 // 调用相应平台微信分享图片方法  
  38.                 if (cc.sys.os === cc.sys.OS_ANDROID) {  
  39.                     jsb.reflection.callStaticMethod('org/cocos2dx/javascript/ShareImage''shareIMG''(Ljava/lang/String;)V', fullPath);  
  40.                     console.log("分享成功!!!");  
  41.                 } else if (cc.sys.os === cc.sys.OS_IOS) {  
  42.                     jsb.reflection.callStaticMethod('AppController''shareIMG:', fullPath);  
  43.                 }  
  44.                 self._isCapturing = false;  
  45.             } else {  
  46.                 tryTimes++;  
  47.                 if (tryTimes > 10) {  
  48.                     self._isCapturing = false;  
  49.                     cc.log('截图失败,超时~');  
  50.                     return;  
  51.                 }  
  52.                 setTimeout(fn, 50);  
  53.             }  
  54.         };  
  55.         setTimeout(fn, 50);  
  56.         console.log("截图完成!!");  
  57.     },  

官方API

安卓部分:

[javascript]  view plain  copy
  1. /** 
  2.  * 分享图片 
  3.  */  
  4. public static void shareIMG(String path) {  
  5.     // 初始化WXImageObject对象  
  6.     Bitmap bmp = BitmapFactory.decodeFile(path);  
  7.     WXImageObject imgObj = new WXImageObject(bmp);  
  8.   
  9.     // 初始化WXMediaMessage对象  
  10.     WXMediaMessage msg = new WXMediaMessage();  
  11.     msg.mediaObject = imgObj;  
  12.       
  13.     // 设置缩略图  
  14.     Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 128, 72, true);  
  15.     bmp.recycle();  
  16.     msg.thumbData = Util.bmpToByteArray(thumbBmp, true); // Util工具类在微信官方的范例代码中  
  17.       
  18.     // 构造一个Req  
  19.     SendMessageToWX.Req req = new SendMessageToWX.Req();  
  20.     req.transaction = buildTransaction("img"); // transaction字段用于唯一标识一个请求  
  21.     req.message = msg;  
  22.     req.scene = SendMessageToWX.Req.WXSceneSession;  
  23.       
  24.     // 调用api接口发送数据到微信  
  25.     api.sendReq(req);  
  26. }  

猜你喜欢

转载自blog.csdn.net/lck8989/article/details/80214043
今日推荐