-
Notifications
You must be signed in to change notification settings - Fork 3
/
SendAgent.mm
executable file
·224 lines (182 loc) · 8.13 KB
/
SendAgent.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#import "SendAgent.h"
#define PrefFilePath @"/var/mobile/Library/Preferences/com.icenuts.photosweibo.image.plist"
static UIBackgroundTaskIdentifier bgTask;
static UIImage* cacheImage;
@implementation SendAgent
@synthesize imagePath;
@synthesize application;
@synthesize enteredText;
@synthesize rawData;
- (void)initWithAppKey:(NSString *)appKey appSecret:(NSString *)appSecret text:(id)mytext imgPath: (id) path{
application = [UIApplication sharedApplication];
//Parse engine
engine = [[WBEngine alloc] initWithAppKey: appKey appSecret: appSecret];
[engine setDelegate: self];
imagePath = [path copy];
enteredText = [mytext copy];
}
- (void)initWithAppKey:(NSString *)appKey appSecret:(NSString *)appSecret text:(id)mytext imgData: (id) data{
application = [UIApplication sharedApplication];
//Parse engine
engine = [[WBEngine alloc] initWithAppKey: appKey appSecret: appSecret];
[engine setDelegate: self];
rawData = [[NSData alloc] initWithData: data];
enteredText = [mytext copy];
}
- (void)sendWithData{
//Background handler
bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
CPDistributedMessagingCenter *center;
center = [CPDistributedMessagingCenter centerNamed:@"com.icenuts.photo2weibo.bannerserver"];
[center sendMessageName:@"com.icenuts.photo2weibo.timeout" userInfo:nil];
//Avoid killing app
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *raw_image = [UIImage imageWithData: rawData];
if([enteredText isEqualToString:@""]){
[engine sendWeiBoWithText: @"分享图片" image: raw_image url:nil];
}else{
[engine sendWeiBoWithText: enteredText image: raw_image url:nil];
}
});
}
- (void) send{
//Background handler
bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
CPDistributedMessagingCenter *center;
center = [CPDistributedMessagingCenter centerNamed:@"com.icenuts.photo2weibo.bannerserver"];
[center sendMessageName:@"com.icenuts.photo2weibo.timeout" userInfo:nil];
//Avoid killing app
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *original = [UIImage imageWithContentsOfFile: imagePath];
CGFloat width = original.size.width;
CGFloat height = original.size.height;
CGFloat ratio = height/width;
id Preferences = [[NSDictionary alloc] initWithContentsOfFile: PrefFilePath];
NSString* state;
int myscale = 600;
if([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] == ReachableViaWiFi){
NSLog(@"-----Wifi-----");
myscale = 600;
state = [Preferences valueForKey:@"PhotoImageWifi"];
if([state isEqualToString: @"low"]){
myscale = 600;
}else if([state isEqualToString: @"medium"]){
myscale = 900;
}else if([state isEqualToString: @"high"]){
myscale = 1400;
}
if(width > myscale){
width = myscale;
height = width*ratio;
}
}else{
myscale = 450;
state = [Preferences valueForKey:@"PhotoImageCarrier"];
if([state isEqualToString: @"low"]){
myscale = 450;
}else if([state isEqualToString: @"medium"]){
myscale = 600;
}else if([state isEqualToString: @"high"]){
myscale = 900;
}
if(width > myscale){
width = myscale;
height = width*ratio;
}
}
CGSize newSize = CGSizeMake(width, height);
UIGraphicsBeginImageContext(newSize);
[original drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* tmp = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Assign Data
cacheImage = [tmp retain];
UIImage *send = nil;
NSArray *suffix = [imagePath componentsSeparatedByString: @"."];
NSString *fix = [[suffix lastObject] lowercaseString];
if(!([fix isEqualToString: @"png"] || [fix isEqualToString: @"jpg"] || [fix isEqualToString: @"gif"])){
NSData *imageData = UIImagePNGRepresentation(tmp);
send = [UIImage imageWithData: imageData];
}
if(send == nil){
send = [tmp retain];
}
if(!imagePath){
[engine sendWeiBoWithText: enteredText image:nil url:nil];
}else if([fix isEqualToString: @"gif"])
{
if([enteredText isEqualToString:@""]){
[engine sendWeiBoWithText: @"分享图片" image:nil url:imagePath];
}
else
{
[engine sendWeiBoWithText: enteredText image:nil url:imagePath];
}
}else{
if([enteredText isEqualToString:@""]){
[engine sendWeiBoWithText: @"分享图片" image:send url:nil];
}else{
[engine sendWeiBoWithText: enteredText image:send url:nil];
}
}
});
}
- (void)engine:(WBEngine *)engine requestDidSucceedWithResult:(id)result
{
SystemSoundID sound;
NSURL *url = [NSURL fileURLWithPath:@"/System/Library/Audio/UISounds/tweet_sent.caf" isDirectory: NO];
AudioServicesCreateSystemSoundID((CFURLRef)url,&sound);
AudioServicesPlaySystemSound(sound);
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: UIImagePNGRepresentation(cacheImage), @"image", enteredText, @"msg",nil ];
CPDistributedMessagingCenter *center;
center = [CPDistributedMessagingCenter centerNamed:@"com.icenuts.photo2weibo.bannerserver"];
[center sendMessageName:@"com.icenuts.photo2weibo.success" userInfo: dictionary];
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
- (void)engine:(WBEngine *)engine requestDidFailWithError:(NSError *)error
{
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: UIImagePNGRepresentation(cacheImage), @"image", enteredText, @"msg",nil ];
SystemSoundID sound;
NSURL *url = [NSURL fileURLWithPath:@"/System/Library/Audio/UISounds/Tink.caf" isDirectory: NO];
AudioServicesCreateSystemSoundID((CFURLRef)url,&sound);
AudioServicesPlaySystemSound(sound);
CPDistributedMessagingCenter *center;
center = [CPDistributedMessagingCenter centerNamed:@"com.icenuts.photo2weibo.bannerserver"];
[center sendMessageName:@"com.icenuts.photo2weibo.failure" userInfo: dictionary];
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
- (void)engineNotAuthorized:(WBEngine *)engine
{
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: UIImagePNGRepresentation(cacheImage), @"image", @"发送失败,您还没有设置帐户", @"msg",nil ];
SystemSoundID sound;
NSURL *url = [NSURL fileURLWithPath:@"/System/Library/Audio/UISounds/Tink.caf" isDirectory: NO];
AudioServicesCreateSystemSoundID((CFURLRef)url,&sound);
AudioServicesPlaySystemSound(sound);
CPDistributedMessagingCenter *center;
center = [CPDistributedMessagingCenter centerNamed:@"com.icenuts.photo2weibo.bannerserver"];
[center sendMessageName:@"com.icenuts.photo2weibo.failure" userInfo: dictionary];
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
- (void)engineAuthorizeExpired:(WBEngine *)engine
{
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: UIImagePNGRepresentation(cacheImage), @"image", @"发送失败,帐户已过期,请重新登陆", @"msg",nil ];
SystemSoundID sound;
NSURL *url = [NSURL fileURLWithPath:@"/System/Library/Audio/UISounds/Tink.caf" isDirectory: NO];
AudioServicesCreateSystemSoundID((CFURLRef)url,&sound);
AudioServicesPlaySystemSound(sound);
CPDistributedMessagingCenter *center;
center = [CPDistributedMessagingCenter centerNamed:@"com.icenuts.photo2weibo.bannerserver"];
[center sendMessageName:@"com.icenuts.photo2weibo.failure" userInfo: dictionary];
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
@end