1010#import " AFNetworking.h"
1111#import " UIImageView+AFNetworking.h"
1212
13- @interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
13+ @interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
1414
15- @property (nonatomic , strong ) NSString * token;
16- @property (nonatomic , strong ) NSString * domain;
17- @property (nonatomic , strong ) UIImage * pickImage;
15+ @property (nonatomic , strong ) NSString *token;
16+ @property (nonatomic , strong ) NSString *domain;
17+ @property (nonatomic , strong ) UIImage *pickImage;
1818
1919@end
2020
@@ -26,128 +26,115 @@ - (void)viewDidLoad {
2626 self.title = @" 七牛云上传" ;
2727}
2828
29- - (IBAction )chooseAction : (id )sender
30- {
29+ - (IBAction )chooseAction : (id )sender {
3130 [self gotoImageLibrary ];
3231}
3332
34- - (IBAction )uploadAction : (id )sender
35- {
33+ - (IBAction )uploadAction : (id )sender {
3634 if (self.pickImage == nil ) {
3735 UIAlertView *alert = [[UIAlertView alloc ]
38- initWithTitle: @" 还未选择图片"
39- message: @" "
40- delegate: nil
41- cancelButtonTitle: @" OK!"
42- otherButtonTitles: nil ];
36+ initWithTitle: @" 还未选择图片"
37+ message: @" "
38+ delegate: nil
39+ cancelButtonTitle: @" OK!"
40+ otherButtonTitles: nil ];
4341 [alert show ];
44- }else
45- {
46- [self getTokenFromQN ];
42+ } else {
43+ [self getTokenFromQN ];
4744 }
4845}
4946
50- - (void )getTokenFromQN
51- {
47+ - (void )getTokenFromQN {
5248 // 1.管理器
5349 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager ];
54-
55- // //2.设置登录参数
56- // NSDictionary *dict = @{ @"username":@"xn", @"password":@"123" };
57-
50+
51+ // //2.设置登录参数
52+ // NSDictionary *dict = @{ @"username":@"xn", @"password":@"123" };
53+
5854 // 3.请求
59- [manager POST: @" http://115.231.183.102:9090/api/quick_start/simple_image_example_token.php" parameters: nil success: ^(AFHTTPRequestOperation *operation, id responseObject) {
55+ [manager POST: @" http://115.231.183.102:9090/api/quick_start/simple_image_example_token.php" parameters: nil success: ^(AFHTTPRequestOperation *operation, id responseObject) {
6056 self.domain = responseObject[@" domain" ];
6157 self.token = responseObject[@" uptoken" ];
6258 [self uploadImageToQNFilePath: [self getImagePath: self .pickImage]];
63- } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
64- NSLog (@" %@ " , error);
65- }];
59+ }
60+ failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
61+ NSLog (@" %@ " , error);
62+ }];
6663}
6764
68- -(void )uploadImageToQNFilePath : (NSString *)filePath
69- {
65+ - (void )uploadImageToQNFilePath : (NSString *)filePath {
7066 QNUploadManager *upManager = [[QNUploadManager alloc ] init ];
71- QNUploadOption * uploadOption = [[QNUploadOption alloc ] initWithMime: nil progressHandler: ^(NSString *key, float percent) {
72- NSLog (@" percent == %.2f " ,percent);
73- } params: nil checkCrc: NO cancellationSignal: nil ];
74- [upManager putFile: filePath key: nil token: self .token complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
67+ QNUploadOption *uploadOption = [[QNUploadOption alloc ] initWithMime: nil progressHandler: ^(NSString *key, float percent) {
68+ NSLog (@" percent == %.2f " , percent);
69+ }
70+ params: nil
71+ checkCrc: NO
72+ cancellationSignal: nil ];
73+ [upManager putFile: filePath key: nil token: self .token complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
7574 NSLog (@" info ===== %@ " , info);
7675 NSLog (@" resp ===== %@ " , resp);
77- NSLog (@" %@ /%@ " ,self.domain ,resp[@" key" ]);
78- [self .preViewImage setImageWithURL: [NSURL URLWithString: [NSString stringWithFormat: @" %@ /%@ " ,self .domain,resp[@" key" ]]] placeholderImage: [UIImage imageNamed: @" placeholder.jpg" ]];
79-
80- } option: uploadOption];
81-
82- }
76+ NSLog (@" %@ /%@ " , self.domain , resp[@" key" ]);
77+ [self .preViewImage setImageWithURL: [NSURL URLWithString: [NSString stringWithFormat: @" %@ /%@ " , self .domain, resp[@" key" ]]] placeholderImage: [UIImage imageNamed: @" placeholder.jpg" ]];
8378
79+ }
80+ option: uploadOption];
81+ }
8482
85- - (void )gotoImageLibrary
86- {
87- if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary])
88- {
83+ - (void )gotoImageLibrary {
84+ if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) {
8985 UIImagePickerController *picker = [[UIImagePickerController alloc ] init ];
9086 picker.delegate = self;
9187 picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
9288 [self presentViewController: picker animated: YES completion: nil ];
93- }else {
89+ } else {
9490 UIAlertView *alert = [[UIAlertView alloc ]
95- initWithTitle: @" 访问图片库错误"
96- message: @" "
97- delegate: nil
98- cancelButtonTitle: @" OK!"
99- otherButtonTitles: nil ];
91+ initWithTitle: @" 访问图片库错误"
92+ message: @" "
93+ delegate: nil
94+ cancelButtonTitle: @" OK!"
95+ otherButtonTitles: nil ];
10096 [alert show ];
10197 }
10298}
10399
104-
105-
106100// 再调用以下委托:
107101#pragma mark UIImagePickerControllerDelegate
108102- (void )imagePickerController : (UIImagePickerController *)picker
109- didFinishPickingImage : (UIImage *)image editingInfo : ( NSDictionary *) editingInfo
110- {
103+ didFinishPickingImage : (UIImage *)image
104+ editingInfo : ( NSDictionary *) editingInfo {
111105 self.pickImage = image; // imageView为自己定义的UIImageView
112106 [picker dismissViewControllerAnimated: YES completion: ^{
113107 }];
114-
115-
116108}
117109
118110- (void )imagePickerControllerDidCancel : (UIImagePickerController *)picker {
119111 [picker dismissViewControllerAnimated: YES completion: nil ];
120-
121112}
122113
123114// 照片获取本地路径转换
124- -(NSString *)getImagePath : (UIImage *)Image
125- {
126- NSString * filePath = nil ;
127- NSData * data = nil ;
128- if (UIImagePNGRepresentation (Image) == nil )
129- {
115+ - (NSString *)getImagePath : (UIImage *)Image {
116+ NSString *filePath = nil ;
117+ NSData *data = nil ;
118+ if (UIImagePNGRepresentation (Image) == nil ) {
130119 data = UIImageJPEGRepresentation (Image, 1.0 );
131- }
132- else
133- {
120+ } else {
134121 data = UIImagePNGRepresentation (Image);
135122 }
136-
123+
137124 // 图片保存的路径
138125 // 这里将图片放在沙盒的documents文件夹中
139- NSString * DocumentsPath = [NSHomeDirectory () stringByAppendingPathComponent: @" Documents" ];
140-
126+ NSString *DocumentsPath = [NSHomeDirectory () stringByAppendingPathComponent: @" Documents" ];
127+
141128 // 文件管理器
142129 NSFileManager *fileManager = [NSFileManager defaultManager ];
143-
130+
144131 // 把刚刚图片转换的data对象拷贝至沙盒中
145132 [fileManager createDirectoryAtPath: DocumentsPath withIntermediateDirectories: YES attributes: nil error: nil ];
146- NSString * ImagePath = [[NSString alloc ]initWithFormat:@" /theFirstImage.png" ];
133+ NSString *ImagePath = [[NSString alloc ] initWithFormat: @" /theFirstImage.png" ];
147134 [fileManager createFileAtPath: [DocumentsPath stringByAppendingString: ImagePath] contents: data attributes: nil ];
148-
135+
149136 // 得到选择后沙盒中图片的完整路径
150- filePath = [[NSString alloc ]initWithFormat:@" %@%@ " ,DocumentsPath,ImagePath];
137+ filePath = [[NSString alloc ] initWithFormat: @" %@%@ " , DocumentsPath, ImagePath];
151138 return filePath;
152139}
153140
0 commit comments