1.获取用户编辑之后的图片
if (editedImage) {
photoPath = [CacheUtil cachePhoto:editedImage];
[self uploadPhoto];
}
2.通过缓存工具取出路径
2.1获取缓存文件的路径
`+ (NSString *)fileDirectory{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString * path = [NSString stringWithFormat:@"/%@",@"photoCache"];
NSString * fileDirectory = [documentDirectory stringByAppendingPathComponent:path];
NSFileManager * fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:fileDirectory]) {
[fileManager createDirectoryAtPath:fileDirectory withIntermediateDirectories:YES attributes:nil error:nil];
}
return fileDirectory;
}`
2.2
新建一个图片的路径,将图片压缩0.5 之后存进一个nsdata 里面。
将这个data写道指定路径的位置,返回这个路径。
+ (NSString *)cachePhoto:(UIImage *)photo{
NSString * photoPath = [[self fileDirectory ] stringByAppendingPathComponent:[photo imageDataMd5]];
//具体这段代码的意义需要再考证
NSData * data = UIImageJPEGRepresentation(photo, 0.5);
[data writeToFile:photoPath atomically:YES];
data = nil;
return photoPath;
}
3.上传这个路径下的文件
3.1 首先从服务端获取token凭证
- (void)uploadFilePath:(NSString *)filePath formatType:(NSString *)formatType sender:(CompletionHandler)handler{
requestHander = handler;
[self requestToken];
m_filePath = filePath;
m_formatType = formatType;
}
3.2
请求token之后执行上传
- (void)requestToken{
[AFHttpTool getTokenSuccess:^(AFHTTPRequestOperation *operation, id response) {
token = response[@"token"];
[self uploadFile];
//上传
} failure:^(id err) {
AFHTTPRequestOperation * op = err;
NSDictionary * response = [op.responseString jsonValue];
if (response[@"error"][@"message"]) {
[[[UIAlertView alloc] initWithTitle:nil message:response[@"error"][@"message"] delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil] show];
}
}];
}
3.3 上传图片到七牛
- (void)uploadFile{
QNUploadManager *upManager = [[QNUploadManager alloc] init];
NSData* data = [NSData dataWithContentsOfFile:m_filePath];
NSArray * tempQinniuStr = [m_filePath componentsSeparatedByString:@"/"];
//将上传过来的路径名分割成数组
//取出最后一个元素作为上传时候的key(相当于在七牛里面的唯一ID);
NSString * qiniuFilePath2 = [NSString stringWithFormat:@"%@",[tempQinniuStr lastObject]];
[upManager putData:data key:qiniuFilePath2 token:token
complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
NSLog(@"%@", info);
NSLog(@"%@", key);
qiniuFilePath = [NSString stringWithFormat:@"%@%@",kQiNiuDomain,key];
requestHander(YES, qiniuFilePath);
//上传成功block返回七牛里面的路径
} option:nil];
}