- 导入SKD
- 封装
前言:公司活动需要加入微信分享,有多处调用,就地封装了。
微信开放平台
1. 导入SDK
1.在微信开放平台添加新的应用获取到AppId
,通过coopods
集成。
2.需要在info
中添加微信分享生产的AppId
然后AppleDelegate
中注册微信
[WXApi registerApp:WXAPPID];
2. 封装
/**
分享消息类型
*/
typedef NS_ENUM(NSInteger,BNNHomeWXShareToolMessageType) {
/** 分享文本信息 */
BNNHomeWXShareToolMessageTypeForTxt = 0,
/** 分享截图 */
BNNHomeWXShareToolMessageTypeForScreenShot = 1,
};
/** 分享目标 */
typedef NS_ENUM(NSInteger,BNNHomeWXShareToolToType) {
/** 分享到朋友圈 */
BNNHomeWXShareToolToTypeForTimeLine = 0,
/** 分享到聊天界面 */
BNNHomeWXShareToolToTypeForSession = 1,
/** 分享到收藏 */
BNNHomeWXShareToolToTypeForFavorite = 2,
};
#if 0
WXSceneSession = 0, /**< 聊天界面 */
WXSceneTimeline = 1, /**< 朋友圈 */
WXSceneFavorite = 2, /**< 收藏 */
#endif
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface BNNHomeWXShareTool : NSObject
+ (BNNHomeWXShareTool *)shareInstance;
/** 分享截图 */
- (void)shareWXWithShareTitle:(NSString *)ShareTitle Detail:(NSString *)shareDetail Image:(NSString *)shareImage Url:(NSString *)shareUrl MessageType:(NSInteger)shareMessageType shareView:(UIView *)shareView toType:(NSInteger)toType;
@end
#import "BNNHomeWXShareTool.h"
@interface BNNApiBNNTool ()
@end
@implementation BNNHomeWXShareTool
/**
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317332&token=217ea698b9763c5cedc3e09ab3f8841bb31cae1b&lang=zh_CN
@return 微信开发平台
*/
+ (BNNHomeWXShareTool *)shareInstance{
static BNNHomeWXShareTool *bnnHomeWXShareTool;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
bnnHomeWXShareTool = [[BNNHomeWXShareTool alloc]init];
});
return bnnHomeWXShareTool;
}
- (void)shareWXWithShareTitle:(NSString *)ShareTitle Detail:(NSString *)shareDetail Image:(NSString *)shareImage Url:(NSString *)shareUrl MessageType:(NSInteger)shareMessageType shareView:(UIView *)shareView toType:(NSInteger)toType{
/** 分享截图 */
if (shareMessageType == BNNHomeWXShareToolMessageTypeForTxt) {
/** 文本 */
/** 网页 */
if (toType == BNNHomeWXShareToolToTypeForTimeLine) {
/** 分享到朋友圈 */
}else if (toType == BNNHomeWXShareToolToTypeForSession){
/** 分享到聊天界面 */
}
[self shareViewWithShareTitle:ShareTitle thumbImage:shareImage Detail:shareDetail Url:shareUrl toType:toType];
}else if (shareMessageType == BNNHomeWXShareToolMessageTypeForScreenShot){
/** 分享截图 */
// [self shareViewWithImage:[self getImage]];
/** 微信分享没有通过先保存截图 */
[self getImage];
/** 保存截图 */
[self shareImageSaveWithshareView:shareView];
}
}
#pragma mark - 保存照片到本地
- (void)shareImageSaveWithshareView:(UIView *)shareView{
UIImage *newImage = [self rendImageWihthView:shareView];
// 2.写入相册
UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
#pragma mark - 屏幕截图 + 保存到相册
- (UIImage *)rendImageWihthView:(UIView *)view{
// 1.开始位图上下文
// UIGraphicsBeginImageContext(view.frame.size);
/** 高清截图 */
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
// 2.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 3.截图
[view.layer renderInContext:ctx];
// 4.获取图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// 5.关闭上下文
UIGraphicsEndImageContext() ;
return newImage;
}
#pragma mark 用来监听图片保存到相册的状况
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
if (error) {
[SVProgressHUD showMessage:@"保存失败"];
}else{
[SVProgressHUD showMessage:@"截图保存成功,您可以分享了~"];
}
NSLog(@"%@",contextInfo);
}
#pragma mark 截取当前屏幕,并生成image对象
-(UIImage *)getImage
{
/** 模糊截图 */
// UIGraphicsBeginImageContext([UIScreen mainScreen].bounds.size);
/** 高清截图 */
UIGraphicsBeginImageContextWithOptions([UIScreen mainScreen].bounds.size, NO, 0.0);
[[UIApplication sharedApplication].windows[0].layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
#pragma mark - 分享网页
- (void)shareViewWithShareTitle:(NSString *)shareTile thumbImage:(NSString *)shareImage Detail:(NSString *)shareDetail Url:(NSString *)shareUrl toType:(NSInteger)toType{
WXWebpageObject *webpageObject = [WXWebpageObject object];
webpageObject.webpageUrl = shareUrl;
WXMediaMessage *message = [WXMediaMessage message];
message.title = shareTile;
message.description = shareDetail;
// [message setThumbImage:[UIImage imageNamed:@"缩略图.jpg"]];
[message setThumbImage:[self getImageFromURL:shareImage]];
message.mediaObject = webpageObject;
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
if (toType == BNNHomeWXShareToolToTypeForTimeLine) {
req.scene = WXSceneTimeline;
}else if (toType == BNNHomeWXShareToolToTypeForSession){
req.scene = WXSceneSession;
}
// req.scene = WXSceneSession;
[WXApi sendReq:req];
}
#pragma mark - 网络图片转UIImage
- (UIImage *) getImageFromURL:(NSString *)fileURL
{
UIImage * result;
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];
return result;
}
#pragma mark - 压缩图片
- (UIImage *)compressImage:(UIImage *)image toByte:(NSUInteger)maxLength {
// Compress by quality
CGFloat compression = 1;
NSData *data = UIImageJPEGRepresentation(image, compression);
if (data.length < maxLength) return image;
CGFloat max = 1;
CGFloat min = 0;
for (int i = 0; i < 6; ++i) {
compression = (max + min) / 2;
data = UIImageJPEGRepresentation(image, compression);
if (data.length < maxLength * 0.9) {
min = compression;
} else if (data.length > maxLength) {
max = compression;
} else {
break;
}
}
UIImage *resultImage = [UIImage imageWithData:data];
if (data.length < maxLength) return resultImage;
// Compress by size
NSUInteger lastDataLength = 0;
while (data.length > maxLength && data.length != lastDataLength) {
lastDataLength = data.length;
CGFloat ratio = (CGFloat)maxLength / data.length;
CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
(NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
UIGraphicsBeginImageContext(size);
[resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
data = UIImageJPEGRepresentation(resultImage, compression);
}
return resultImage;
}
调用:
[[BNNHomeWXShareTool shareInstance] shareWXWithShareTitle:self.shareTitle Detail:self.shareDetail Image:self.shareImage Url:self.shareUrl MessageType:BNNHomeWXShareToolMessageTypeForTxt shareView:self.view toType:BNNHomeWXShareToolToTypeForTimeLine];