TCBasePlayViewController.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // TCBasePlayViewController.m
  3. // TCLVBIMDemo
  4. //
  5. // Created by annidyfeng on 2017/9/15.
  6. // Copyright © 2017年 tencent. All rights reserved.
  7. //
  8. #import "TCBasePlayViewController.h"
  9. #import <UShareUI/UMSocialUIManager.h>
  10. #import <mach/mach.h>
  11. #import <UIImageView+WebCache.h>
  12. #import "BGBaseAppDelegate.h"
  13. #import "TCConstants.h"
  14. #import <Accelerate/Accelerate.h>
  15. #import <UShareUI/UMSocialUIManager.h>
  16. //#import "TCLoginModel.h"
  17. #import "NSString+Common.h"
  18. #import "TCVideoPublishController.h"
  19. #import "TCUserInfoModel.h"
  20. #import "TCUtil.h"
  21. NSString *const kTCLivePlayError = @"kTCLivePlayError";
  22. @interface TCBasePlayViewController ()
  23. @end
  24. @implementation TCBasePlayViewController
  25. -(id)initWithPlayInfo:(TCLiveInfo *)info videoIsReady:(videoIsReadyBlock)videoIsReady
  26. {
  27. self = [super init];
  28. if (self) {
  29. _videoIsReady = videoIsReady;
  30. _liveInfo = info;
  31. }
  32. return self;
  33. }
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. // Do any additional setup after loading the view.
  37. }
  38. - (void)didReceiveMemoryWarning {
  39. [super didReceiveMemoryWarning];
  40. // Dispose of any resources that can be recreated.
  41. }
  42. /*
  43. #pragma mark - Navigation
  44. // In a storyboard-based application, you will often want to do a little preparation before navigation
  45. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  46. // Get the new view controller using [segue destinationViewController].
  47. // Pass the selected object to the new view controller.
  48. }
  49. */
  50. //创建高斯模糊效果图片
  51. -(UIImage *)gsImage:(UIImage *)image withGsNumber:(CGFloat)blur
  52. {
  53. if (blur < 0.f || blur > 1.f) {
  54. blur = 0.5f;
  55. }
  56. int boxSize = (int)(blur * 40);
  57. boxSize = boxSize - (boxSize % 2) + 1;
  58. CGImageRef img = image.CGImage;
  59. vImage_Buffer inBuffer, outBuffer;
  60. vImage_Error error;
  61. void *pixelBuffer;
  62. //从CGImage中获取数据
  63. CGDataProviderRef inProvider = CGImageGetDataProvider(img);
  64. CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
  65. //设置从CGImage获取对象的属性
  66. inBuffer.width = CGImageGetWidth(img);
  67. inBuffer.height = CGImageGetHeight(img);
  68. inBuffer.rowBytes = CGImageGetBytesPerRow(img);
  69. inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);
  70. pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
  71. if(pixelBuffer == NULL)
  72. NSLog(@"No pixelbuffer");
  73. outBuffer.data = pixelBuffer;
  74. outBuffer.width = CGImageGetWidth(img);
  75. outBuffer.height = CGImageGetHeight(img);
  76. outBuffer.rowBytes = CGImageGetBytesPerRow(img);
  77. error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
  78. if (error) {
  79. NSLog(@"error from convolution %ld", error);
  80. }
  81. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  82. CGContextRef ctx = CGBitmapContextCreate( outBuffer.data, outBuffer.width, outBuffer.height, 8, outBuffer.rowBytes, colorSpace, kCGImageAlphaNoneSkipLast);
  83. CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
  84. UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
  85. //clean up
  86. CGContextRelease(ctx);
  87. CGColorSpaceRelease(colorSpace);
  88. free(pixelBuffer);
  89. CFRelease(inBitmapData);
  90. CGColorSpaceRelease(colorSpace);
  91. CGImageRelease(imageRef);
  92. return returnImage;
  93. }
  94. /**
  95. *缩放图片
  96. */
  97. -(UIImage*)scaleImage:(UIImage *)image scaleToSize:(CGSize)size{
  98. UIGraphicsBeginImageContext(size);
  99. [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
  100. UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  101. UIGraphicsEndImageContext();
  102. return scaledImage;
  103. }
  104. /**
  105. *裁剪图片
  106. */
  107. -(UIImage *)clipImage:(UIImage *)image inRect:(CGRect)rect{
  108. CGImageRef sourceImageRef = [image CGImage];
  109. CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
  110. UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
  111. CGImageRelease(newImageRef);
  112. return newImage;
  113. }
  114. /**
  115. @method 获取指定宽度width的字符串在UITextView上的高度
  116. @param textView 待计算的UITextView
  117. @param Width 限制字符串显示区域的宽度
  118. @result float 返回的高度
  119. */
  120. - (float) heightForString:(UITextView *)textView andWidth:(float)width{
  121. CGSize sizeToFit = [textView sizeThatFits:CGSizeMake(width, MAXFLOAT)];
  122. return sizeToFit.height;
  123. }
  124. - (void) toastTip:(NSString*)toastInfo
  125. {
  126. CGRect frameRC = [[UIScreen mainScreen] bounds];
  127. frameRC.origin.y = frameRC.size.height - 110;
  128. frameRC.size.height -= 110;
  129. __block UITextView * toastView = [[UITextView alloc] init];
  130. toastView.editable = NO;
  131. toastView.selectable = NO;
  132. frameRC.size.height = [self heightForString:toastView andWidth:frameRC.size.width];
  133. toastView.frame = frameRC;
  134. toastView.text = toastInfo;
  135. toastView.backgroundColor = [UIColor whiteColor];
  136. toastView.alpha = 0.5;
  137. [self.view addSubview:toastView];
  138. dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC);
  139. dispatch_after(popTime, dispatch_get_main_queue(), ^(){
  140. [toastView removeFromSuperview];
  141. toastView = nil;
  142. });
  143. }
  144. - (void)shareLive {
  145. __weak typeof(self) weakSelf = self;
  146. //显示分享面板
  147. [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
  148. [weakSelf shareDataWithPlatform:platformType];
  149. }];
  150. }
  151. - (void)shareDataWithPlatform:(UMSocialPlatformType)platformType
  152. {
  153. // 创建UMSocialMessageObject实例进行分享
  154. // 分享数据对象
  155. UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
  156. NSString *title = self.liveInfo.title;
  157. NSString *url = [NSString stringWithFormat:@"%@?userid=%@&type=%d&fileid=%@&ts=%@&sdkappid=%@&acctype=%@",
  158. kLivePlayShareAddr,
  159. TC_PROTECT_STR([self.liveInfo.userid stringByUrlEncoding]),
  160. 1,
  161. TC_PROTECT_STR([self.liveInfo.fileid stringByUrlEncoding]),
  162. [NSString stringWithFormat:@"%d", self.liveInfo.timestamp],
  163. [[TCUserInfoModel sharedInstance] getUserProfile].appid,
  164. [[TCUserInfoModel sharedInstance] getUserProfile].accountType];
  165. NSString *text = [NSString stringWithFormat:NSLocalizedString(@"TCBasePlayView.LiveBroadcast", nil), self.liveInfo.userinfo.nickname ? self.liveInfo.userinfo.nickname : self.liveInfo.userid];
  166. /* 以下分享类型,开发者可根据需求调用 */
  167. // 1、纯文本分享
  168. messageObject.text = NSLocalizedString(@"TCBasePlayView.LiveBroadcastNotify", nil);
  169. // 2、 图片或图文分享
  170. // 图片分享参数可设置URL、NSData类型
  171. // 注意:由于iOS系统限制(iOS9+),非HTTPS的URL图片可能会分享失败
  172. UMShareImageObject *shareObject = [UMShareImageObject shareObjectWithTitle:title descr:text thumImage:self.liveInfo.userinfo.frontcover];
  173. [shareObject setShareImage:self.liveInfo.userinfo.frontcoverImage];
  174. UMShareWebpageObject *share2Object = [UMShareWebpageObject shareObjectWithTitle:title descr:text thumImage:self.liveInfo.userinfo.frontcoverImage];
  175. share2Object.webpageUrl = url;
  176. //新浪微博有个bug,放在shareObject里面设置url,分享到网页版的微博不显示URL链接,这里在text后面也加上链接
  177. if (platformType == UMSocialPlatformType_Sina) {
  178. messageObject.text = [NSString stringWithFormat:@"%@ %@",messageObject.text,share2Object.webpageUrl];
  179. }else{
  180. messageObject.shareObject = share2Object;
  181. }
  182. [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
  183. NSString *message = nil;
  184. if (!error) {
  185. message = [NSString stringWithFormat:NSLocalizedString(@"TCBasePlayView.ShareSucceeded", nil)];
  186. } else {
  187. if (error.code == UMSocialPlatformErrorType_Cancel) {
  188. message = [NSString stringWithFormat:NSLocalizedString(@"TCBasePlayView.ShareCanceled", nil)];
  189. } else if (error.code == UMSocialPlatformErrorType_NotInstall) {
  190. message = [NSString stringWithFormat:NSLocalizedString(@"TCBasePlayView.AppNotInstalled", nil)];
  191. } else {
  192. message = [NSString stringWithFormat:NSLocalizedString(@"TCBasePlayView.ShareFailed", nil),(int)error.code];
  193. }
  194. }
  195. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
  196. message:message
  197. delegate:nil
  198. cancelButtonTitle:NSLocalizedString(@"Common.OK", nil)
  199. otherButtonTitles:nil];
  200. [alert show];
  201. }];
  202. }
  203. @end