123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- //
- // XYSocialShareHandler.m
- // Starbuds
- //
- // Created by 翟玉磊 on 2020/1/19.
- // Copyright © 2020 翟玉磊. All rights reserved.
- //
- #import "XYSocialShareHandler.h"
- #import "WXApi.h"
- #import "WeiboSDK.h"
- @interface XYSocialShareHandler ()<WBHttpRequestDelegate, WBMediaTransferProtocol>
- @end
- @implementation XYSocialShareHandler
- + (instancetype)sharedInstance {
- static XYSocialShareHandler *shareManager;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- shareManager = [[XYSocialShareHandler alloc] init];
- });
- return shareManager;
- }
- - (instancetype)init {
- if (self = [super init]) {
-
- }
- return self;
- }
- /**
- 分享给微信好友图片
-
- @param image 原图
- @param thumImage 缩略图
- */
- - (void)share_WXImageWithImage:(UIImage *)image thumbImage:(UIImage *)thumImage {
-
- /// 大小不能超过64K
- UIImage *thumbUrlImage = [UIImage imageWithMaxSide:100.0f sourceImage:image];
-
- WXMediaMessage *message = [WXMediaMessage message];
- [message setThumbImage:thumbUrlImage];
-
- WXImageObject *imageObject = [WXImageObject object];
- imageObject.imageData = UIImageJPEGRepresentation(image, 1);
- message.mediaObject = imageObject;
-
- SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
- req.bText = NO;
- req.message = message;
- req.scene = WXSceneSession; //分享到微信好友
- [WXApi sendReq:req completion:nil];
- }
- /**
- 分享给微信朋友圈图片
-
- @param image 原图
- @param thumImage 缩略图
- */
- - (void)share_PYQImageWithImage:(UIImage *)image thumbImage:(UIImage *)thumImage {
-
- /// 大小不能超过64K
- UIImage *thumbUrlImage = [UIImage imageWithMaxSide:100.0f sourceImage:image];
-
- WXMediaMessage *message = [WXMediaMessage message];
- [message setThumbImage:thumbUrlImage];
-
- WXImageObject *imageObject = [WXImageObject object];
- imageObject.imageData = UIImageJPEGRepresentation(image, 1);
- message.mediaObject = imageObject;
-
- SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
- req.bText = NO;
- req.message = message;
- req.scene = WXSceneTimeline; //分享到微信朋友圈
- [WXApi sendReq:req completion:nil];
- }
- /**
- 分享给微信好友链接
- @param title 标题
- @param description 描述
- @param image 缩略图
- @param url url
- */
- - (void)share_WXURLWithTitle:(NSString *)title description:(NSString *)description thumbImage:(UIImage *)image url:(NSString *)url {
-
- /// 大小不能超过64K
- UIImage *thumbUrlImage = [UIImage imageWithMaxSide:80.0f sourceImage:image];
-
- WXMediaMessage *message = [WXMediaMessage message];
- message.title = title;
- message.description = description;
- [message setThumbImage:thumbUrlImage];
-
- WXWebpageObject *webpageObject = [WXWebpageObject object];
- webpageObject.webpageUrl = url;
- message.mediaObject = webpageObject;
-
- SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
- req.bText = NO;
- req.message = message;
- req.scene = WXSceneSession;
-
- [WXApi sendReq:req completion:nil];
- }
- /**
- 分享给微信朋友圈链接
-
- @param title 标题
- @param description 描述
- @param image 缩略图
- @param url url
- */
- - (void)share_PYQURLWithTitle:(NSString *)title description:(NSString *)description thumbImage:(UIImage *)image url:(NSString *)url {
-
- /// 大小不能超过64K
- UIImage *thumbUrlImage = [UIImage imageWithMaxSide:80.0f sourceImage:image];
-
- WXMediaMessage *message = [WXMediaMessage message];
- message.title = title;
- message.description = description;
- [message setThumbImage:thumbUrlImage];
-
- WXWebpageObject *webpageObject = [WXWebpageObject object];
- webpageObject.webpageUrl = url;
- message.mediaObject = webpageObject;
-
- SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
- req.bText = NO;
- req.message = message;
- req.scene = WXSceneTimeline;
-
- [WXApi sendReq:req completion:nil];
- }
- /**
- 分享到小程序
- @param title 标题
- @param description 副标题
- @param webpageUrl 兼容低版本的网页链接
- @param path 小程序的页面路径
- @param hdImage 小程序新版本的预览图二进制数据
- */
- - (void)share_miniProgramWithTitle:(NSString *)title description:(NSString *)description webpageUrl:(NSString *)webpageUrl path:(NSString *)path hdImage:(UIImage *)hdImage {
-
- // /// 等比例压缩
- // CGFloat imagWidth = 200.0f;
- // hdImage = [UIImage imageCompressForSize:hdImage targetSize:CGSizeMake(imagWidth, imagWidth/5*4)];
- // NSData *imageData = UIImageJPEGRepresentation(hdImage, 1);
- NSData *imageData = [self resetSizeOfImageData:hdImage maxSize:128];
- NSData *urlimageData = [self resetSizeOfImageData:hdImage maxSize:32];
- // if (imageData.length/1024 > 32) {
- // /// url分享的图片需要小于32k
- // UIImage *thumbUrlImage = [UIImage imageWithMaxSide:50.0f sourceImage:hdImage];
- // urlimageData = UIImageJPEGRepresentation(thumbUrlImage, 1);
- // }
- /// 小程序原始id
- NSString *userName = @"gh_7c6684f2120b";
- WXMiniProgramObject *object = [WXMiniProgramObject object];
- /// 兼容低版本的网页链接 限制长度不超过10KB
- object.webpageUrl = webpageUrl;
- /// 小程序的userName 小程序原始ID获取方法:登录小程序管理后台-设置-基本设置-帐号信息
- object.userName = userName;
- /// 小程序的页面路径
- object.path = path;
- /// 小程序新版本的预览图二进制数据,6.5.9及以上版本微信客户端支持
- /// 限制大小不超过128KB,自定义图片建议长宽比是 5:4。
- /// 大小不能超过128k
- object.hdImageData = imageData;
- /// 是否使用带shareTicket的分享
- object.withShareTicket = NO;
- /// 小程序的类型,默认正式版,1.8.1及以上版本开发者工具包支持分享开发版和体验版小程序
- if ([ZYLService currentService] == ZYLServiceEnvironmentRelease) {
- object.miniProgramType = WXMiniProgramTypeRelease;
- }else {
- object.miniProgramType = WXMiniProgramTypePreview;
- }
-
- WXMediaMessage *message = [WXMediaMessage message];
- message.title = title;
- message.description = description;
- message.thumbData = urlimageData; //兼容旧版本节点的图片,小于32KB,新版本优先
- //使用WXMiniProgramObject的hdImageData属性
- message.mediaObject = object;
-
- SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
- req.bText = NO;
- req.message = message;
- req.scene = WXSceneSession; //目前只支持会话
- [WXApi sendReq:req completion:nil];
- }
- /**
- 分享文字到新浪微博
- @param text 要分享的文字
- @return 是否成功
- */
- - (BOOL)share_sinaTextWithText:(NSString *)text {
-
- WBMessageObject *message = [WBMessageObject message];
- message.text = text;
-
- WBProvideMessageForWeiboResponse *response = [WBProvideMessageForWeiboResponse responseWithMessage:message];
-
- return [WeiboSDK sendResponse:response];
- }
- /**
- 分享图片到新浪微博
-
- @param images 要分享的图片数组
- @return 是否成功
- */
- - (BOOL)share_sinaImageWithImages:(NSArray *)images {
-
- WBMessageObject *message = [WBMessageObject message];
-
- WBImageObject *imageObject = [WBImageObject object];
- imageObject.isShareToStory = YES;
- imageObject.delegate = self;
- [imageObject addImages:images];
- message.imageObject = imageObject;
-
- WBProvideMessageForWeiboResponse *response = [WBProvideMessageForWeiboResponse responseWithMessage:message];
-
- return [WeiboSDK sendResponse:response];
- }
- /**
- 分享链接到新浪微博
-
- @param url 要分享的链接
- @return 是否成功
- */
- - (BOOL)share_sinaUrlWithUrl:(NSString *)url title:(NSString *)title description:(NSString *)description thumbImage:(UIImage *)image {
-
- WBMessageObject *message = [WBMessageObject message];
-
- WBWebpageObject *webpage = [WBWebpageObject object];
- webpage.objectID = @"identifier1";
- webpage.title = NSLocalizedString(title, nil);
- webpage.description = [NSString stringWithFormat:NSLocalizedString(description, nil), [[NSDate date] timeIntervalSince1970]];
- webpage.thumbnailData = UIImagePNGRepresentation(image);
- webpage.webpageUrl = url;
- message.mediaObject = webpage;
-
- WBProvideMessageForWeiboResponse *response = [WBProvideMessageForWeiboResponse responseWithMessage:message];
-
- return [WeiboSDK sendResponse:response];
- }
- /**
- 分享视频到新浪微博
-
- @param url 要分享的视频链接
- @return 是否成功
- */
- - (BOOL)share_sinaVideoWithVideoUrl:(NSString *)url title:(NSString *)title description:(NSString *)description thumbImage:(UIImage *)image {
-
- WBMessageObject *message = [WBMessageObject message];
-
- WBNewVideoObject *videoObject = [WBNewVideoObject object];
- videoObject.isShareToStory = YES;
- NSURL *videoUrl = [NSURL URLWithString:url];
- videoObject.delegate = self;
- [videoObject addVideo:videoUrl];
- message.videoObject = videoObject;
-
- WBProvideMessageForWeiboResponse *response = [WBProvideMessageForWeiboResponse responseWithMessage:message];
-
- return [WeiboSDK sendResponse:response];
- }
- /**
- 分享纯文本到QQ
- @param text 要分享的text
- @param type 1QQ好友 2QQ空间
- @return 分享回调状态
- */
- - (QQApiSendResultCode)share_QQTextWithText:(NSString *)text shareType:(NSInteger)type {
- QQApiTextObject *txtObj = [QQApiTextObject objectWithText:text];
- SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:txtObj];
- if (type == 1) {
- //将内容分享到qq
- return [QQApiInterface sendReq:req];
- }else {
- //将内容分享到qzone
- return [QQApiInterface SendReqToQZone:req];
- }
- }
- /**
- 分享图片到QQ
-
- @param image 要分享的图片
- @param title 标题
- @param description 副标题
- @param type 1QQ好友 2QQ空间
- @return 分享回调状态
- */
- - (QQApiSendResultCode)share_QQImageWithImage:(UIImage *)image title:(NSString *)title description:(NSString *)description shareType:(NSInteger)type {
- NSData *imgData = UIImagePNGRepresentation(image);
- QQApiImageObject *imgObj = [QQApiImageObject objectWithData:imgData
- previewImageData:imgData
- title:title
- description:description];
- SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:imgObj];
- if (type == 1) {
- //将内容分享到qq
- return [QQApiInterface sendReq:req];
- }else {
- //将内容分享到qzone
- return [QQApiInterface SendReqToQZone:req];
- }
- }
- /**
- 分享url到QQ
-
- @param url 要分享的url
- @param title 标题
- @param description 副标题
- @param imageUrl 图片url
- @param type 1QQ好友 2QQ空间
- @return 分享回调状态
- */
- - (QQApiSendResultCode)share_QQUrlWithUrl:(NSString *)url title:(NSString *)title description:(NSString *)description previewImageURL:(NSString *)imageUrl shareType:(NSInteger)type {
- NSArray *arr = [url componentsSeparatedByString:@"&t"];
- url = [arr firstObject];
-
- QQApiNewsObject *newsObj = [QQApiNewsObject
- objectWithURL:[NSURL URLWithString:url]
- title:title
- description:description
- previewImageURL:[NSURL URLWithString:imageUrl]];
- SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
-
- if (type == 1) {
- //将内容分享到qq
- return [QQApiInterface sendReq:req];
- }else {
- //将内容分享到qzone
- return [QQApiInterface SendReqToQZone:req];
- }
- }
- /**
- 分享视频到QQ
-
- @param videoUrl 要分享的视频url
- @param title 标题
- @param description 副标题
- @param image 图片
- @param type 1QQ好友 2QQ空间
- @return 分享回调状态
- */
- - (QQApiSendResultCode)share_QQVideoWithVideoUrl:(NSString *)videoUrl title:(NSString *)title description:(NSString *)description previewImage:(UIImage *)image shareType:(NSInteger)type {
- NSData* previewData = UIImagePNGRepresentation(image);
- QQApiVideoObject *videoObj = [QQApiVideoObject objectWithURL:[NSURL URLWithString:videoUrl ? : @""]
- title:title
- description:description
- previewImageData:previewData];
- [videoObj setFlashURL:[NSURL URLWithString:videoUrl]];
- SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:videoObj];
- if (type == 1) {
- //将内容分享到qq
- return [QQApiInterface sendReq:req];
- }else {
- //将内容分享到qzone
- return [QQApiInterface SendReqToQZone:req];
- }
- }
- #pragma mark - WBHttpRequestDelegate
- - (void)request:(WBHttpRequest *)request didFinishLoadingWithResult:(NSString *)result {
-
- NSLog(@"result:%@", result);
- }
- - (void)request:(WBHttpRequest *)request didFailWithError:(NSError *)error {
- NSLog(@"error:%@", error);
- }
- #pragma mark - WBMediaTransferProtocol
- /**
- 数据准备成功回调
- */
- -(void)wbsdk_TransferDidReceiveObject:(id)object {
- NSLog(@"object:%@", object);
- }
- /**
- 数据准备失败回调
- */
- -(void)wbsdk_TransferDidFailWithErrorCode:(WBSDKMediaTransferErrorCode)errorCode andError:(NSError*)error {
- NSLog(@"error:%@", error);
- }
- #pragma mark - 图片压缩
- - (NSData *)resetSizeOfImageData:(UIImage *)sourceImage maxSize:(NSInteger)maxSize {
-
- //先判断当前质量是否满足要求,不满足再进行压缩
- __block NSData *finallImageData = UIImageJPEGRepresentation(sourceImage, 1.0);
- NSUInteger sizeOrigin = finallImageData.length;
- NSUInteger sizeOriginKB = sizeOrigin / 1000;
-
- if (sizeOriginKB <= maxSize) {
- return finallImageData;
- }
-
- //获取原图片宽高比
- CGFloat sourceImageAspectRatio = sourceImage.size.width / sourceImage.size.height;
- //先调整分辨率
- CGSize defaultSize = CGSizeMake(1024, 1024 / sourceImageAspectRatio);
- UIImage * newImage = [self newSizeImage:defaultSize image:sourceImage];
- finallImageData = UIImageJPEGRepresentation(newImage, 1.0);
-
- //保存压缩系数
- NSMutableArray *compressionQualityArr = [NSMutableArray array];
- CGFloat avg = 1.0 / 250;
- CGFloat value = avg;
- for (int i = 250; i >= 1; i --) {
- value = i * avg;
- [compressionQualityArr addObject:@(value)];
- }
-
- /*
- 调整大小
- 说明:压缩系数数组compressionQualityArr是从大到小存储。
- */
- //思路:使用二分法搜索
- finallImageData = [self halfFuntion:compressionQualityArr image:newImage sourceData:finallImageData maxSize:maxSize];
- //如果还是未能压缩到指定大小,则进行降分辨率
- while (finallImageData.length == 0) {
- //每次降100分辨率
- CGFloat reduceWidth = 100.0;
- CGFloat reduceHeight = 100.0 / sourceImageAspectRatio;
- if (defaultSize.width - reduceWidth <= 0 || defaultSize.height - reduceHeight <= 0) {
- break;
- }
- defaultSize = CGSizeMake(defaultSize.width - reduceWidth, defaultSize.height - reduceHeight);
- UIImage *image = [self newSizeImage:defaultSize
- image:[UIImage imageWithData:UIImageJPEGRepresentation(newImage, [[compressionQualityArr lastObject] floatValue])]];
- finallImageData = [self halfFuntion:compressionQualityArr image:image sourceData:UIImageJPEGRepresentation(image,1.0) maxSize:maxSize];
- }
- return finallImageData;
- }
- #pragma mark 调整图片分辨率/尺寸(等比例缩放)
- - (UIImage *)newSizeImage:(CGSize)size image:(UIImage *)sourceImage {
-
- CGSize newSize = CGSizeMake(sourceImage.size.width, sourceImage.size.height);
- CGFloat tempHeight = newSize.height / size.height;
- CGFloat tempWidth = newSize.width / size.width;
-
- if (tempWidth > 1.0 && tempWidth > tempHeight) {
- newSize = CGSizeMake(sourceImage.size.width / tempWidth, sourceImage.size.height / tempWidth);
- } else if (tempHeight > 1.0 && tempWidth < tempHeight) {
- newSize = CGSizeMake(sourceImage.size.width / tempHeight, sourceImage.size.height / tempHeight);
- }
-
- UIGraphicsBeginImageContext(newSize);
- [sourceImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
- UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return newImage;
- }
- #pragma mark 二分法
- - (NSData *)halfFuntion:(NSArray *)arr image:(UIImage *)image sourceData:(NSData *)finallImageData maxSize:(NSInteger)maxSize {
-
- NSData *tempData = [NSData data];
- NSUInteger start = 0;
- NSUInteger end = arr.count - 1;
- NSUInteger index = 0;
- NSUInteger difference = NSIntegerMax;
-
- while(start <= end) {
- index = start + (end - start) / 2;
- finallImageData = UIImageJPEGRepresentation(image, [arr[index] floatValue]);
- NSUInteger sizeOrigin = finallImageData.length;
- NSUInteger sizeOriginKB = sizeOrigin / 1024;
- NSLog(@"当前降到的质量:%ld", (unsigned long)sizeOriginKB);
- NSLog(@"\nstart:%zd\nend:%zd\nindex:%zd\n压缩系数:%lf", start, end, (unsigned long)index, [arr[index] floatValue]);
-
- if (sizeOriginKB > maxSize) {
- start = index + 1;
- } else if (sizeOriginKB < maxSize) {
- if (maxSize - sizeOriginKB < difference) {
- difference = maxSize - sizeOriginKB;
- tempData = finallImageData;
- }
- if (index <= 0) {
- break;
- }
- end = index - 1;
- } else {
- break;
- }
- }
- return tempData;
- }
- @end
|