Skip to content

Commit

Permalink
Merge pull request 100apps#20 from xdongdan/master
Browse files Browse the repository at this point in the history
Thanks to @xdongdan 将OSMessage的image/thumbnail修改为UIImage类型,添加file属性,用于微信分享文件/gif
  • Loading branch information
100apps committed Oct 19, 2015
2 parents cb46728 + 5b9ace8 commit a11d78b
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 27 deletions.
11 changes: 6 additions & 5 deletions OpenShareDemo/openshare/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ @interface ViewController ()
@implementation ViewController{
NSDictionary *icons;
UIScrollView *panel;
NSData *testImage,*testThumbImage,*testGifImage,*testFile;
UIImage *testImage,*testThumbImage;
NSData *testGifImage,*testFile;
}

- (void)viewDidLoad {
[super viewDidLoad];
//初始化测试数据
testImage = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Default@2x" ofType:@"png"]];
testThumbImage= [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"logo" ofType:@"png"]];
testImage = [UIImage imageNamed:@"Default"];//[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Default@2x" ofType:@"png"]];
testThumbImage= [UIImage imageNamed:@"logo"];//[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"logo" ofType:@"png"]];
testGifImage= [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"logo" ofType:@"gif"]];
testFile= [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"]];

Expand Down Expand Up @@ -412,11 +413,11 @@ -(void)weixinViewHandler:(UIButton*)btn{
msg.thumbnail=testThumbImage;
}else if (btn.tag==30008) {
//gif表情/同图片,只是格式是gif。
msg.image=testGifImage;
msg.file =testGifImage;
msg.thumbnail=testThumbImage;
}else if (btn.tag==30009) {
//file
msg.image=testFile;
msg.file=testFile;
msg.thumbnail=testThumbImage;
msg.title=@"test.pdf";//添加到收藏的时候,微信会根据文件名打开。fileExt信息丢失。微信的bug
msg.fileExt=@"pdf";
Expand Down
6 changes: 3 additions & 3 deletions openshare/OpenShare+QQ.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ +(NSString*)genShareUrl:(OSMessage*)msg to:(int)shareTo{
[ret appendString:[self base64AndUrlEncode:msg.title]];
}else if([msg isEmpty:@[@"link"] AndNotEmpty:@[@"title",@"image",@"desc"]]){
//图片分享
NSDictionary *data=@{@"file_data":msg.image,
@"previewimagedata":msg.thumbnail?:msg.image
NSDictionary *data=@{@"file_data":[self dataWithImage:msg.image],
@"previewimagedata":msg.thumbnail? [self dataWithImage:msg.thumbnail] :[self dataWithImage:msg.image scale:CGSizeMake(36, 36)]
};
[self setGeneralPasteboard:@"com.tencent.mqq.api.apiLargeData" Value:data encoding: OSPboardEncodingKeyedArchiver];
[ret appendString:@"img&title="];
Expand All @@ -103,7 +103,7 @@ +(NSString*)genShareUrl:(OSMessage*)msg to:(int)shareTo{
[ret appendString:[self base64Encode:msg.desc]];
}else if ([msg isEmpty:nil AndNotEmpty:@[@"title",@"desc",@"image",@"link",@"multimediaType"]]) {
//新闻/多媒体分享(图片加链接)发送新闻消息 预览图像数据,最大1M字节 URL地址,必填,最长512个字符 via QQApiInterfaceObject.h
NSDictionary *data=@{@"previewimagedata":msg.image};
NSDictionary *data=@{@"previewimagedata":[self dataWithImage:msg.image]};
[self setGeneralPasteboard:@"com.tencent.mqq.api.apiLargeData" Value:data encoding: OSPboardEncodingKeyedArchiver];
NSString *msgType=@"news";
if (msg.multimediaType==OSMultimediaTypeAudio) {
Expand Down
10 changes: 5 additions & 5 deletions openshare/OpenShare+Renren.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,25 @@ +(NSString*)genRenrenShareUrl:(OSMessage*)msg to:(int)shareTo{
NSMutableDictionary *dic=[[NSMutableDictionary alloc] initWithDictionary:@{@"title":msg.title}];
if(msg.multimediaType==OSMultimediaTypeAudio){
dic[@"description"]=msg.desc?:msg.title;
dic[@"thumbData"]=msg.thumbnail?:msg.image;
dic[@"thumbData"]= msg.thumbnail? [self dataWithImage:msg.thumbnail]:[self dataWithImage:msg.image scale:CGSizeMake(36, 36)];
dic[@"url"]=msg.link;
msgType=@"Voice";
}else if(msg.multimediaType==OSMultimediaTypeVideo){
dic[@"description"]=msg.desc?:msg.title;
dic[@"thumbData"]=msg.thumbnail?:msg.image;
dic[@"thumbData"]= msg.thumbnail? [self dataWithImage:msg.thumbnail]:[self dataWithImage:msg.image scale:CGSizeMake(36, 36)];
dic[@"url"]=msg.link;
msgType=@"Video";
}else{
if ([msg isEmpty:nil AndNotEmpty:@[@"image",@"link"]]) {
//图文
dic[@"description"]=msg.desc?:msg.title;
dic[@"thumbData"]=msg.thumbnail?:msg.image;
dic[@"thumbData"]= msg.thumbnail? [self dataWithImage:msg.thumbnail]:[self dataWithImage:msg.image scale:CGSizeMake(36, 36)];
dic[@"url"]=msg.link;
msgType=@"ImgText";
}else if ([msg isEmpty:@[@"link"] AndNotEmpty:@[@"image"]]) {
//图片
dic[@"imageData"]=msg.image;
dic[@"thumbData"]=msg.thumbnail?:msg.image;
dic[@"imageData"]= [self dataWithImage:msg.image];
dic[@"thumbData"]= msg.thumbnail? [self dataWithImage:msg.thumbnail]:[self dataWithImage:msg.image scale:CGSizeMake(36, 36)];
msgType=@"ImgText";
}else if ([msg isEmpty:@[@"link"] AndNotEmpty:@[@"image"]]) {
//文本
Expand Down
4 changes: 2 additions & 2 deletions openshare/OpenShare+Weibo.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ +(void)shareToWeibo:(OSMessage*)msg Success:(shareSuccess)success Fail:(shareFai
message=@{
@"__class" : @"WBMessageObject",
@"imageObject":@{
@"imageData":msg.image
@"imageData":[self dataWithImage:msg.image]
},
@"text" : msg.title
};
Expand All @@ -45,7 +45,7 @@ +(void)shareToWeibo:(OSMessage*)msg Success:(shareSuccess)success Fail:(shareFai
@"__class" : @"WBWebpageObject",
@"description": msg.desc?:msg.title,
@"objectID" : @"identifier1",
@"thumbnailData":msg.thumbnail?:msg.image,
@"thumbnailData":msg.thumbnail ? [self dataWithImage:msg.thumbnail] : [self dataWithImage:msg.image scale:CGSizeMake(100, 100)],
@"title": msg.title,
@"webpageUrl":msg.link
}
Expand Down
25 changes: 15 additions & 10 deletions openshare/OpenShare+Weixin.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,54 +50,59 @@ +(NSString*)genWeixinShareUrl:(OSMessage*)msg to:(int)shareTo{
}
if (!msg.multimediaType) {
//不指定类型
if ([msg isEmpty:@[@"image",@"link"] AndNotEmpty:@[@"title"]]) {
if ([msg isEmpty:@[@"image",@"link", @"file"] AndNotEmpty:@[@"title"]]) {
//文本
dic[@"command"]=@"1020";
dic[@"title"]=msg.title;
}else if([msg isEmpty:@[@"link"] AndNotEmpty:@[@"image"]]){
//图片
dic[@"fileData"]=msg.image;
dic[@"thumbData"]=msg.thumbnail?:msg.image;
dic[@"fileData"]= [self dataWithImage:msg.image];
dic[@"thumbData"]=msg.thumbnail ? [self dataWithImage:msg.thumbnail] : [self dataWithImage:msg.image scale:CGSizeMake(100, 100)];
dic[@"objectType"]=@"2";
}else if([msg isEmpty:nil AndNotEmpty:@[@"link",@"title",@"image"]]){
//有链接。
dic[@"description"]=msg.desc?:msg.title;
dic[@"mediaUrl"]=msg.link;
dic[@"objectType"]=@"5";
dic[@"thumbData"]=msg.thumbnail?:msg.image;
dic[@"thumbData"]=msg.thumbnail? [self dataWithImage:msg.thumbnail]:[self dataWithImage:msg.image scale:CGSizeMake(100, 100)];
dic[@"title"] =msg.title;
} else if ([msg isEmpty:@[@"link"] AndNotEmpty:@[@"file"]]) {
//gif
dic[@"fileData"]= msg.file ? msg.file : [self dataWithImage:msg.image];
dic[@"thumbData"]=msg.thumbnail ? [self dataWithImage:msg.thumbnail] : [self dataWithImage:msg.image scale:CGSizeMake(100, 100)];
dic[@"objectType"]=@"2";
}
}else if(msg.multimediaType==OSMultimediaTypeAudio){
//music
dic[@"description"]=msg.desc?:msg.title;
dic[@"mediaUrl"]=msg.link;
dic[@"mediaDataUrl"]=msg.mediaDataUrl;
dic[@"objectType"]=@"3";
dic[@"thumbData"]=msg.thumbnail?:msg.image;
dic[@"thumbData"]=msg.thumbnail? [self dataWithImage:msg.thumbnail]:[self dataWithImage:msg.image scale:CGSizeMake(100, 100)];;
dic[@"title"] =msg.title;
}else if(msg.multimediaType==OSMultimediaTypeVideo){
//video
dic[@"description"]=msg.desc?:msg.title;
dic[@"mediaUrl"]=msg.link;
dic[@"objectType"]=@"4";
dic[@"thumbData"]=msg.thumbnail?:msg.image;
dic[@"thumbData"]=msg.thumbnail? [self dataWithImage:msg.thumbnail]:[self dataWithImage:msg.image scale:CGSizeMake(100, 100)];;
dic[@"title"] =msg.title;
}else if(msg.multimediaType==OSMultimediaTypeApp){
//app
dic[@"description"]=msg.desc?:msg.title;
if(msg.extInfo)dic[@"extInfo"]=msg.extInfo;
dic[@"fileData"]=msg.image;
dic[@"fileData"]=[self dataWithImage:msg.image];
dic[@"mediaUrl"]=msg.link;
dic[@"objectType"]=@"7";
dic[@"thumbData"]=msg.thumbnail?:msg.image;
dic[@"thumbData"]=msg.thumbnail? [self dataWithImage:msg.thumbnail]:[self dataWithImage:msg.image scale:CGSizeMake(100, 100)];;
dic[@"title"] =msg.title;
}else if(msg.multimediaType==OSMultimediaTypeFile){
//file
dic[@"description"]=msg.desc?:msg.title;
dic[@"fileData"]=msg.image;
dic[@"fileData"]=msg.file;
dic[@"objectType"]=@"6";
dic[@"fileExt"]=msg.fileExt?:@"";
dic[@"thumbData"]=msg.thumbnail?:msg.image;
dic[@"thumbData"]=msg.thumbnail? [self dataWithImage:msg.thumbnail]:[self dataWithImage:msg.image scale:CGSizeMake(100, 100)];;
dic[@"title"] =msg.title;
}
NSData *output=[NSPropertyListSerialization dataWithPropertyList:@{[self keyFor:schema][@"appid"]:dic} format:NSPropertyListBinaryFormat_v1_0 options:0 error:nil];
Expand Down
8 changes: 6 additions & 2 deletions openshare/OpenShare.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ typedef enum : NSUInteger {
@property NSString* title;
@property NSString* desc;
@property NSString* link;
@property NSData* image;
@property NSData* thumbnail;
@property UIImage *image;
@property UIImage *thumbnail;
@property OSMultimediaType multimediaType;
//for 微信
@property NSString* extInfo;
@property NSString* mediaDataUrl;
@property NSString* fileExt;
@property (nonatomic, strong) NSData *file; /// 微信分享gif/文件
/**
* 判断emptyValueForKeys的value都是空的,notEmptyValueForKeys的value都不是空的。
*
Expand Down Expand Up @@ -137,6 +138,9 @@ typedef enum : NSUInteger {
+(paySuccess)paySuccessCallback;
+(payFail)payFailCallback;

+ (NSData *)dataWithImage:(UIImage *)image;
+ (NSData *)dataWithImage:(UIImage *)image scale:(CGSize)size;

@end


21 changes: 21 additions & 0 deletions openshare/OpenShare.m
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,27 @@ + (UIImage *)screenshot
UIGraphicsEndImageContext();
return image;
}

+ (NSData *)dataWithImage:(UIImage *)image {
return UIImageJPEGRepresentation(image, 1);
}

+ (NSData *)dataWithImage:(UIImage *)image scale:(CGSize)size {
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0,0, size.width, size.height)];
UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return UIImageJPEGRepresentation(scaledImage, 1);
}

- (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)size{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0,0, size.width, size.height)];
UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}

@end

@implementation OSMessage
Expand Down

0 comments on commit a11d78b

Please sign in to comment.