JXShareMVc.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. [SVProgressHUD show];
  44. [g_server getAppResource:@"6" ToView:self];
  45. long time = (long)[[NSDate date] timeIntervalSince1970];
  46. time = (time *1000 + g_server.timeDifference);
  47. NSString *salt = [NSString stringWithFormat:@"%ld", time];
  48. [g_server getact_getinviteget:salt andUserId:g_server.myself.userId andToView:self];
  49. }
  50. #pragma mark --- 请求成功
  51. -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  52. [SVProgressHUD dismiss];
  53. //NSLog(@"dict: %@ --- arry: %@",dict ,array1 );
  54. if([aDownload.action isEqualToString:act_getAppResource]){
  55. NSDictionary *dict=[array1 firstObject];
  56. NSString *string=dict[@"link"];
  57. //创建过滤器
  58. CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
  59. //过滤器恢复默认
  60. [filter setDefaults];
  61. //给过滤器添加数据<字符串长度893>
  62. NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
  63. [filter setValue:data forKey:@"inputMessage"];
  64. //获取二维码过滤器生成二维码
  65. CIImage *image = [filter outputImage];
  66. UIImage *img = [self createNonInterpolatedUIImageFromCIImage:image WithSize:140];
  67. //[self.shaview.inviteBtn setImage:img forState:UIControlStateNormal] ;
  68. }
  69. else if([aDownload.action isEqualToString:act_getinviteget])
  70. {
  71. NSDictionary *dictM=dict;
  72. NSString *str=[NSString stringWithFormat:@" 邀请码:%@ ",dictM[@"invite"]];
  73. [self.inviteBtn setTitle:str forState:UIControlStateNormal];
  74. }
  75. }
  76. -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  77. [SVProgressHUD dismiss];
  78. NSString * errorCode = [NSString stringWithFormat:@"%@",[dict objectForKey:@"resultMsg"]];
  79. if([errorCode isEqualToString:@"权限验证失败"])
  80. {
  81. if ([aDownload.action isEqualToString:act_GetBalanceRedTimes])
  82. {
  83. }else{
  84. }
  85. }
  86. return [errorCode intValue];
  87. }
  88. //分享
  89. - (void)saveBttIMG1:(UIButton *)sender{
  90. if (sender.tag==0) {
  91. [self shareAction:
  92. @[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" ];
  93. } if (sender.tag==1) {
  94. [self shareAction:
  95. @[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" ];
  96. } if (sender.tag==2) {
  97. 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";
  98. NSString *yaoqingM =@"";//self.jixinNoBtn.titleLabel.text;
  99. NSString *dicript = [NSString stringWithFormat:@"您的邀请码是:%@%@",yaoqingM,@" 即信—安全稳定的赚钱平台 \n给你推荐一款可以赚钱的APP。我今天又收入了999元,现在还有3个名额,快进来下载一起赚钱吧!"];
  100. UIImage *shareImage = [UIImage imageNamed:@"ALOGO_120"];
  101. NSURL *url = [NSURL fileURLWithPath:musicPath1];
  102. NSArray * activityItems = @[dicript,shareImage,url];
  103. UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
  104. //去除一些不需要的图标选项
  105. activityVC.excludedActivityTypes = @[UIActivityTypePostToFacebook, UIActivityTypeAirDrop, UIActivityTypePostToWeibo, UIActivityTypePostToTencentWeibo];
  106. //成功失败的回调block
  107. UIActivityViewControllerCompletionWithItemsHandler myBlock = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError) {
  108. if (completed){
  109. }else{
  110. }
  111. };
  112. activityVC.completionWithItemsHandler = myBlock;
  113. [self presentViewController:activityVC animated:YES completion:nil];
  114. }
  115. if (sender.tag==3) {
  116. UIImage *saveIMG = [self captureImageFromView:self.view];
  117. UIImageWriteToSavedPhotosAlbum(saveIMG, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
  118. }
  119. }
  120. - (void)saveBttIMG:(UIButton *)showBt{
  121. UIImage *saveIMG = [self captureImageFromView:self.view];
  122. UIImageWriteToSavedPhotosAlbum(saveIMG, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
  123. }
  124. -(UIImage *)captureImageFromView:(UIView *)view{
  125. UIGraphicsBeginImageContextWithOptions(self.view.frame.size,NO, 0);
  126. [[UIColor clearColor] setFill];
  127. [[UIBezierPath bezierPathWithRect:self.view.bounds] fill];
  128. CGContextRef ctx = UIGraphicsGetCurrentContext();
  129. [self.view.layer renderInContext:ctx];
  130. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  131. UIGraphicsEndImageContext();
  132. return image;
  133. }
  134. //下面得到分享完成的回调
  135. -(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
  136. {
  137. NSLog(@"didFinishGetUMSocialDataInViewController with response is %@",response);
  138. //根据`responseCode`得到发送结果,如果分享成功
  139. if(response.responseCode == UMSResponseCodeSuccess)
  140. {
  141. //得到分享到的微博平台名
  142. NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
  143. }
  144. }
  145. - (void)shareAction:(NSArray *)type pathUrl:(NSString *)musicPath {
  146. //[self shareAction: @[ UMShareToQzone,UMShareToQQ,UMShareToTencent] pathUrl: ];
  147. 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";
  148. NSString *yaoqingM =@"";//self.jixinNoBtn.titleLabel.text;
  149. NSString *dicript = [NSString stringWithFormat:@"您的邀请码是:%@%@",yaoqingM,@" 即信—安全稳定的赚钱平台 \n给你推荐一款可以赚钱的APP。我今天又收入了999元,现在还有3个名额,快进来下载一起赚钱吧!"];
  150. UIImage *shareImage = [UIImage imageNamed:@"ALOGO_120"];
  151. NSURL *url = [NSURL fileURLWithPath:musicPath];
  152. NSArray * activityItems = @[dicript,musicPath];
  153. UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
  154. //去除一些不需要的图标选项
  155. activityVC.excludedActivityTypes = @[UIActivityTypePostToFacebook, UIActivityTypeAirDrop, UIActivityTypePostToWeibo, UIActivityTypePostToTencentWeibo];
  156. //成功失败的回调block
  157. UIActivityViewControllerCompletionWithItemsHandler myBlock = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError) {
  158. if (completed){
  159. }else{
  160. }
  161. };
  162. activityVC.completionWithItemsHandler = myBlock;
  163. [self presentViewController:activityVC animated:YES completion:nil];
  164. return;
  165. NSString *shareText = dicript; //分享内嵌文字
  166. [UMSocialSnsService presentSnsIconSheetView:self appKey:@"5edaacbe978eea085d11d331" shareText:shareText shareImage:shareImage shareToSnsNames:type delegate:self];
  167. return;
  168. }
  169. - (UIImage *)createNonInterpolatedUIImageFromCIImage:(CIImage *)image WithSize:(CGFloat)size
  170. {
  171. CGRect extent = CGRectIntegral(image.extent);
  172. CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
  173. //创建bitmap
  174. size_t width = CGRectGetWidth(extent)*scale;
  175. size_t height = CGRectGetHeight(extent)*scale;
  176. CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
  177. CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
  178. CIContext *context = [CIContext contextWithOptions:nil];
  179. CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
  180. CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
  181. CGContextScaleCTM(bitmapRef, scale, scale);
  182. CGContextDrawImage(bitmapRef, extent, bitmapImage);
  183. //保存图片
  184. CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
  185. CGContextRelease(bitmapRef);
  186. CGImageRelease(bitmapImage);
  187. return [UIImage imageWithCGImage:scaledImage];
  188. }
  189. #pragma mark -- <保存到相册>
  190. -(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
  191. NSString *msg = nil ;
  192. if(error){
  193. msg = @"保存图片失败" ;
  194. }else{
  195. [SVProgressHUD showWithStatus:@"保存成功"];
  196. [SVProgressHUD dismissWithDelay:1.0];
  197. msg = @"保存图片成功" ;
  198. }
  199. }
  200. @end