#import "AddImgsViewController.h"
#import "ImagePicker.h"
#import "UploadImageManager.h"
#import "NetworkManager.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "QNUploadManager.h"
#import "QiniuSDK.h"
#import "NetworkManager.h"
@interface AddImgsViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *img;
@property (nonatomic, strong) NSString *token;
@end
@implementation AddImgsViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self loadSenvenToken];
}
- (void)loadSenvenToken{
NSString *url = [NSString stringWithFormat:@"%@%@",URL_Base,URL_CommentsToken];
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSString *apikey = [NSString stringWithFormat:@"%@",[user objectForKey:@"apikey"]];
[NetworkManager requestGETWithURLStr:url paramDic:nil Api_key:apikey finish:^(id responseObject) {
NSLog(@"token请求成功%@",responseObject);
self.token = [responseObject objectForKey:@"token"];
} enError:^(NSError *error) {
NSLog(@"失败%@",error);
}];
}
- (IBAction)addImgBtn:(id)sender {
[self gotoImageLibrary];
}
- (IBAction)uploadAction:(UIButton *)sender {
if (self.img.image == nil) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"还未选择图片"
message:@""
delegate:nil
cancelButtonTitle:@"OK!"
otherButtonTitles:nil];
[alert show];
} else {
[self uploadImageToQNFilePath:[self getImagePath:self.img.image]];
}
}
- (void)uploadImageToQNFilePath:(NSString *)filePath {
// self.token = @"你的token";
QNUploadManager *upManager = [[QNUploadManager alloc] init];
QNUploadOption *uploadOption = [[QNUploadOption alloc] initWithMime:nil progressHandler:^(NSString *key, float percent) {
NSLog(@"percent == %.2f", percent);
}
params:nil
checkCrc:NO
cancellationSignal:nil];
[upManager putFile:filePath key:nil token:self.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
NSLog(@"info ===== %@", info);
NSLog(@"resp ===== %@", resp);
}
option:uploadOption];
}
- (void)gotoImageLibrary {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"访问图片库错误"
message:@""
delegate:nil
cancelButtonTitle:@"OK!"
otherButtonTitles:nil];
[alert show];
}
}
//再调用以下委托:
#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
self.img.image = image; //imageView为自己定义的UIImageView
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
//照片获取本地路径转换
- (NSString *)getImagePath:(UIImage *)Image {
NSString *filePath = nil;
NSData *data = nil;
if (UIImagePNGRepresentation(Image) == nil) {
data = UIImageJPEGRepresentation(Image, 1.0);
} else {
data = UIImagePNGRepresentation(Image);
}
//图片保存的路径
//这里将图片放在沙盒的documents文件夹中
NSString *DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
//把刚刚图片转换的data对象拷贝至沙盒中
[fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
NSString *ImagePath = [[NSString alloc] initWithFormat:@"/theFirstImage.png"];
[fileManager createFileAtPath:[DocumentsPath stringByAppendingString:ImagePath] contents:data attributes:nil];
//得到选择后沙盒中图片的完整路径
filePath = [[NSString alloc] initWithFormat:@"%@%@", DocumentsPath, ImagePath];
return filePath;
}
iOS-七牛云上传图片
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 七牛云每上传一张图片都要从服务端请求token,和key不能像阿里云那样在客户端sdk中设置路径。如果要上传多张图...
- 用CocoaPods导入SDK 导入头文件 官网获取两个key 获取token 项目中一般是由服务器来生成,这里...
- 七牛云是一个便捷的数据云存储平台。通过官方或社区社区SDK你可以方便通过程序上传文件到自己的空间中。下面的这个小 ...
- // 第一步 七牛云官网配置 // 第二步 代码上传 ① 七牛云配置 ② 文件上传的service相关 ③ ser...