七牛云储存理解
2016-03-31 18:57
281 查看
1.获取用户编辑之后的图片
2.通过缓存工具取出路径
2.1获取缓存文件的路径
2.2
新建一个图片的路径,将图片压缩0.5 之后存进一个nsdata 里面。
将这个data写道指定路径的位置,返回这个路径。
3.上传这个路径下的文件
3.1 首先从服务端获取token凭证
3.2
请求token之后执行上传
3.3 上传图片到七牛
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]; }
相关文章推荐
- 虚函数和纯虚函数的区别
- 11个让你吃惊的 Linux 终端命令
- 多线程获取不到HttpContext
- 面试-阿里
- andorid 菜单 进度条
- 使用ngxtop统计nginx请求
- 使用批处理获取拖拽文件的文件名+扩展名
- Linux 命令使用
- 算法问题(2)输出全排列
- An error has occurred while drawing:java.lang.IllegalStateException: The display list is not valid.
- 如何对singleInstance的Activity多次传入Intent
- 学到一个android小应用-霓虹灯闪烁效果界面
- 22.4-5拓扑排序BFS
- 格雷码和二进制码的转换
- 新框架(BSF_JAVA)环境部署个人总结
- 144. Binary Tree Preorder Traversal
- Ubuntu15.10安装Arduino IDE记录
- Activity之Notification(通知)
- 使用Collections.sort()实现List正反排序的方法
- JavaScript 基础知识点