JXShareMVc.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //
  2. // JXShareMVc.m
  3. // shiku_im
  4. //
  5. // Created by os on 2020/7/7.
  6. // Copyright © 2020 Reese. All rights reserved.
  7. //
  8. #import "JXShareMVc.h"
  9. #import "UMSocialSnsService.h"
  10. #import "UMSocialSnsPlatformManager.h"
  11. #import "MyButton.h"
  12. @interface JXShareMVc ()
  13. @property (weak, nonatomic) IBOutlet UIButton *inviteBtn;
  14. @property (weak, nonatomic) IBOutlet UIImageView *shareIMG;
  15. @property (weak, nonatomic) IBOutlet UIImageView *topIMGView;
  16. @property (weak, nonatomic) IBOutlet UIImageView *shareKIMG;
  17. @end
  18. @implementation JXShareMVc
  19. - (IBAction)backBtn:(id)sender {
  20. [g_navigation dismissViewController:self animated:YES];
  21. }
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. NSArray *imageS=@[@"circle_of_friends_icon",@"微信",@"qq好友",@"保存图片"];
  25. NSArray *titleS=@[@"微信朋友圈",@"微信好友",@"QQ好友",@"保存图片"];
  26. for (int i=0; i<4; i++) {
  27. MyButton *bottomBtn = [[MyButton alloc]init];
  28. //bottomBtn.backgroundColor=kRGBColor246;
  29. bottomBtn.tag=i;
  30. //[bottomBtn setTitleColor:kRGBColor(222, 29, 19) forState:UIControlStateNormal];
  31. [bottomBtn setImage:[UIImage imageNamed:imageS[i]] forState:UIControlStateNormal];
  32. [bottomBtn setTitle:titleS[i] forState:UIControlStateNormal];
  33. [bottomBtn addTarget:self action:@selector(saveBttIMG1:) forControlEvents:UIControlEventTouchUpInside];
  34. [_shareKIMG addSubview:bottomBtn];
  35. //bottomBtn.frame=CGRectMake(i*((JX_SCREEN_WIDTH-60)/4), 0, (JX_SCREEN_WIDTH-60)/4, 60);
  36. [bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  37. make.left.mas_equalTo(1+i*((JX_SCREEN_WIDTH-60)/4));
  38. make.width.mas_equalTo((JX_SCREEN_WIDTH-60)/4);
  39. // make.height.mas_equalTo(60);
  40. make.top.mas_equalTo(1);
  41. }];
  42. }
  43. UISwipeGestureRecognizer *recognizer2 = [[UISwipeGestureRecognizer alloc]
  44. initWithTarget:self
  45. action:@selector(foundSwipe2:)];
  46. recognizer2.direction =UISwipeGestureRecognizerDirectionDown;
  47. [_topIMGView addGestureRecognizer:recognizer2];
  48. [SVProgressHUD show];
  49. [g_server getAppResource:@"6" ToView:self];
  50. long time = (long)[[NSDate date] timeIntervalSince1970];
  51. time = (time *1000 + g_server.timeDifference);
  52. NSString *salt = [NSString stringWithFormat:@"%ld", time];
  53. [g_server getact_getinviteget:salt andUserId:g_server.myself.userId andToView:self];
  54. }
  55. - (void)foundSwipe:(UISwipeGestureRecognizer *)sender {
  56. CGRect bounds=_shareIMG.bounds;
  57. bounds.origin.y=110;
  58. _shareIMG.bounds=bounds;
  59. }
  60. - (void)foundSwipe2:(UISwipeGestureRecognizer *)sender {
  61. //设置识别滑动方向
  62. sender.direction =UISwipeGestureRecognizerDirectionDown;
  63. //Swipe手势识别器关联到View
  64. [self.view addGestureRecognizer:sender];
  65. CGRect bounds=self.view.bounds;
  66. bounds.origin.y=0;
  67. _shareIMG.bounds=bounds;
  68. }
  69. #pragma mark --- 请求成功
  70. -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  71. [SVProgressHUD dismiss];
  72. //NSLog(@"dict: %@ --- arry: %@",dict ,array1 );
  73. if([aDownload.action isEqualToString:act_getAppResource]){
  74. NSDictionary *dict=[array1 firstObject];
  75. NSString *string=dict[@"link"];
  76. //创建过滤器
  77. CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
  78. //过滤器恢复默认
  79. [filter setDefaults];
  80. //给过滤器添加数据<字符串长度893>
  81. NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
  82. [filter setValue:data forKey:@"inputMessage"];
  83. //获取二维码过滤器生成二维码
  84. CIImage *image = [filter outputImage];
  85. UIImage *img = [self createNonInterpolatedUIImageFromCIImage:image WithSize:140];
  86. //[self.shaview.inviteBtn setImage:img forState:UIControlStateNormal] ;
  87. }
  88. else if([aDownload.action isEqualToString:act_getinviteget])
  89. {
  90. NSDictionary *dictM=dict;
  91. NSString *str=[NSString stringWithFormat:@" 邀请码:%@ ",dictM[@"invite"]];
  92. [self.inviteBtn setTitle:str forState:UIControlStateNormal];
  93. }
  94. }
  95. -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  96. [SVProgressHUD dismiss];
  97. NSString * errorCode = [NSString stringWithFormat:@"%@",[dict objectForKey:@"resultMsg"]];
  98. if([errorCode isEqualToString:@"权限验证失败"])
  99. {
  100. if ([aDownload.action isEqualToString:act_GetBalanceRedTimes])
  101. {
  102. }else{
  103. }
  104. }
  105. return [errorCode intValue];
  106. }
  107. //分享
  108. - (void)saveBttIMG1:(UIButton *)sender{
  109. if (sender.tag==0) {
  110. [self shareAction:
  111. @[UMShareToWechatTimeline] pathUrl:@"https://apps.apple.com/cn/app/%E8%81%8A%E5%95%B5-%E4%B8%8E%E6%9C%8B%E5%8F%8B%E4%B8%80%E8%B5%B7%E7%95%85%E8%81%8A/id1463401862" ];
  112. } if (sender.tag==1) {
  113. [self shareAction:
  114. @[UMShareToWechatSession] pathUrl:@"https://apps.apple.com/cn/app/%E8%81%8A%E5%95%B5-%E4%B8%8E%E6%9C%8B%E5%8F%8B%E4%B8%80%E8%B5%B7%E7%95%85%E8%81%8A/id1463401862" ];
  115. } if (sender.tag==2) {
  116. NSString *musicPath1=@"https://apps.apple.com/cn/app/%E8%81%8A%E5%95%B5-%E4%B8%8E%E6%9C%8B%E5%8F%8B%E4%B8%80%E8%B5%B7%E7%95%85%E8%81%8A/id1463401862";
  117. NSString *yaoqingM =@"";//self.jixinNoBtn.titleLabel.text;
  118. NSString *dicript = [NSString stringWithFormat:@"您的邀请码是:%@%@",yaoqingM,@" 即信—安全稳定的赚钱平台 \n给你推荐一款可以赚钱的APP。我今天又收入了999元,现在还有3个名额,快进来下载一起赚钱吧!"];
  119. UIImage *shareImage = [UIImage imageNamed:@"ALOGO_1200"];
  120. NSURL *url = [NSURL fileURLWithPath:musicPath1];
  121. NSArray * activityItems = @[dicript,shareImage,url];
  122. UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
  123. //去除一些不需要的图标选项
  124. activityVC.excludedActivityTypes = @[UIActivityTypePostToFacebook, UIActivityTypeAirDrop, UIActivityTypePostToWeibo, UIActivityTypePostToTencentWeibo];
  125. //成功失败的回调block
  126. UIActivityViewControllerCompletionWithItemsHandler myBlock = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError) {
  127. if (completed){
  128. }else{
  129. }
  130. };
  131. activityVC.completionWithItemsHandler = myBlock;
  132. [self presentViewController:activityVC animated:YES completion:nil];
  133. }
  134. if (sender.tag==3) {
  135. UIImage *saveIMG = [self captureImageFromView:self.view];
  136. UIImageWriteToSavedPhotosAlbum(saveIMG, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
  137. }
  138. }
  139. - (void)saveBttIMG:(UIButton *)showBt{
  140. UIImage *saveIMG = [self captureImageFromView:self.view];
  141. UIImageWriteToSavedPhotosAlbum(saveIMG, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
  142. }
  143. -(UIImage *)captureImageFromView:(UIView *)view{
  144. UIGraphicsBeginImageContextWithOptions(self.view.frame.size,NO, 0);
  145. [[UIColor clearColor] setFill];
  146. [[UIBezierPath bezierPathWithRect:self.view.bounds] fill];
  147. CGContextRef ctx = UIGraphicsGetCurrentContext();
  148. [self.view.layer renderInContext:ctx];
  149. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  150. UIGraphicsEndImageContext();
  151. return image;
  152. }
  153. //下面得到分享完成的回调
  154. -(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
  155. {
  156. NSLog(@"didFinishGetUMSocialDataInViewController with response is %@",response);
  157. //根据`responseCode`得到发送结果,如果分享成功
  158. if(response.responseCode == UMSResponseCodeSuccess)
  159. {
  160. //得到分享到的微博平台名
  161. NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
  162. }
  163. }
  164. - (void)shareAction:(NSArray *)type pathUrl:(NSString *)musicPath {
  165. //[self shareAction: @[ UMShareToQzone,UMShareToQQ,UMShareToTencent] pathUrl: ];
  166. musicPath=@"https://apps.apple.com/cn/app/%E8%81%8A%E5%95%B5-%E4%B8%8E%E6%9C%8B%E5%8F%8B%E4%B8%80%E8%B5%B7%E7%95%85%E8%81%8A/id1463401862";
  167. NSString *yaoqingM =@"";//self.jixinNoBtn.titleLabel.text;
  168. NSString *dicript = [NSString stringWithFormat:@"您的邀请码是:%@%@",yaoqingM,@" 即信—安全稳定的赚钱平台 \n给你推荐一款可以赚钱的APP。我今天又收入了999元,现在还有3个名额,快进来下载一起赚钱吧!"];
  169. UIImage *shareImage = [UIImage imageNamed:@"ALOGO_1200"];
  170. NSURL *url = [NSURL fileURLWithPath:musicPath];
  171. NSArray * activityItems = @[dicript,musicPath];
  172. UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
  173. //去除一些不需要的图标选项
  174. activityVC.excludedActivityTypes = @[UIActivityTypePostToFacebook, UIActivityTypeAirDrop, UIActivityTypePostToWeibo, UIActivityTypePostToTencentWeibo];
  175. //成功失败的回调block
  176. UIActivityViewControllerCompletionWithItemsHandler myBlock = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError) {
  177. if (completed){
  178. }else{
  179. }
  180. };
  181. activityVC.completionWithItemsHandler = myBlock;
  182. [self presentViewController:activityVC animated:YES completion:nil];
  183. return;
  184. NSString *shareText = dicript; //分享内嵌文字
  185. [UMSocialSnsService presentSnsIconSheetView:self appKey:@"5edaacbe978eea085d11d331" shareText:shareText shareImage:shareImage shareToSnsNames:type delegate:self];
  186. return;
  187. }
  188. - (UIImage *)createNonInterpolatedUIImageFromCIImage:(CIImage *)image WithSize:(CGFloat)size
  189. {
  190. CGRect extent = CGRectIntegral(image.extent);
  191. CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
  192. //创建bitmap
  193. size_t width = CGRectGetWidth(extent)*scale;
  194. size_t height = CGRectGetHeight(extent)*scale;
  195. CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
  196. CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
  197. CIContext *context = [CIContext contextWithOptions:nil];
  198. CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
  199. CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
  200. CGContextScaleCTM(bitmapRef, scale, scale);
  201. CGContextDrawImage(bitmapRef, extent, bitmapImage);
  202. //保存图片
  203. CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
  204. CGContextRelease(bitmapRef);
  205. CGImageRelease(bitmapImage);
  206. return [UIImage imageWithCGImage:scaledImage];
  207. }
  208. #pragma mark -- <保存到相册>
  209. -(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
  210. NSString *msg = nil ;
  211. if(error){
  212. msg = @"保存图片失败" ;
  213. }else{
  214. [SVProgressHUD showWithStatus:@"保存成功"];
  215. [SVProgressHUD dismissWithDelay:1.0];
  216. msg = @"保存图片成功" ;
  217. }
  218. }
  219. @end