66// Copyright © 2016年 Aaron. All rights reserved.
77//
88
9+ #import " Configure.h" // 测试参数配置,暂时只有token,可删除
10+
911#import " ViewController.h"
1012#import " QNTransactionManager.h"
1113
14+ typedef NS_ENUM (NSInteger , UploadState){
15+ UploadStatePrepare,
16+ UploadStateUploading,
17+ UploadStateCancelling
18+ };
19+
1220@interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
1321
1422@property (nonatomic , weak ) IBOutlet UIButton* chooseBtn;
1523@property (nonatomic , weak ) IBOutlet UIButton* uploadBtn;
1624@property (nonatomic , weak ) IBOutlet UIImageView* preViewImage;
1725@property (weak , nonatomic ) IBOutlet UIProgressView *progressView;
1826
27+ @property (nonatomic , assign ) UploadState uploadState;
1928@property (nonatomic , strong ) NSString *token;
2029@property (nonatomic , strong ) UIImage *pickImage;
2130
@@ -26,25 +35,56 @@ @implementation ViewController
2635- (void )viewDidLoad {
2736 [super viewDidLoad ];
2837 // Do any additional setup after loading the view, typically from a nib.
38+ [self changeUploadState: UploadStatePrepare];
2939 self.title = @" 七牛云上传" ;
3040}
3141
3242- (IBAction )chooseAction : (id )sender {
3343 [self gotoImageLibrary ];
3444}
3545
36- - (IBAction )uploadAction : (id )sender {
37- if (self.pickImage == nil ) {
38- [self alertMessage: @" 还未选择图片" ];
46+ - (IBAction )uploadAction : (UIButton *)sender {
47+ if (self.uploadState == UploadStatePrepare) {
48+
49+ #ifdef YourToken
50+ NSString *path = [[NSBundle mainBundle ] pathForResource: @" UploadResource.dmg" ofType: nil ];
51+ [self uploadImageToQNFilePath: path];
52+ [self changeUploadState: UploadStateUploading];
53+ #else
54+ if (self.pickImage == nil ) {
55+ [self alertMessage: @" 还未选择图片" ];
56+ } else {
57+ [self uploadImageToQNFilePath: [self getImagePath: self .pickImage]];
58+ [self changeUploadState: UploadStateUploading];
59+ }
60+ #endif
61+
3962 } else {
40- [self uploadImageToQNFilePath: [self getImagePath: self .pickImage]];
63+ [self changeUploadState: UploadStateCancelling];
64+ }
65+ }
66+
67+ - (void )changeUploadState : (UploadState)uploadState {
68+
69+ self.uploadState = uploadState;
70+ if (uploadState == UploadStatePrepare) {
71+ [self .uploadBtn setTitle: @" 上传" forState: UIControlStateNormal];
72+ self.uploadBtn .enabled = true ;
73+ } else if (uploadState == UploadStateUploading) {
74+ [self .uploadBtn setTitle: @" 取消上传" forState: UIControlStateNormal];
75+ self.uploadBtn .enabled = true ;
76+ } else {
77+ [self .uploadBtn setTitle: @" 取消上传" forState: UIControlStateNormal];
78+ self.uploadBtn .enabled = false ;
4179 }
4280}
4381
4482- (void )uploadImageToQNFilePath : (NSString *)filePath {
45- self.token = @" 你的token" ;
83+
84+ self.token = YourToken;
4685 QNConfiguration *configuration = [QNConfiguration build: ^(QNConfigurationBuilder *builder) {
4786 builder.useConcurrentResumeUpload = true ;
87+ builder.recorder = [QNFileRecorder fileRecorderWithFolder: [NSHomeDirectory () stringByAppendingPathComponent: @" Documents" ] error: nil ];
4888 }];
4989 QNUploadManager *upManager = [[QNUploadManager alloc ] initWithConfiguration: configuration];
5090
@@ -55,11 +95,16 @@ - (void)uploadImageToQNFilePath:(NSString *)filePath {
5595 }
5696 params: nil
5797 checkCrc: NO
58- cancellationSignal: nil ];
59- [upManager putFile: filePath key: nil token: self .token complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
98+ cancellationSignal: ^BOOL {
99+ return weakSelf.uploadState == UploadStateCancelling;
100+ }];
101+
102+ [upManager putFile: filePath key: @" DemoResource" token: self .token complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
60103 NSLog (@" info ===== %@ " , info);
61104 NSLog (@" resp ===== %@ " , resp);
62- [self alertMessage: info.message];
105+
106+ [weakSelf changeUploadState: UploadStatePrepare];
107+ [weakSelf alertMessage: info.message];
63108 }
64109 option: uploadOption];
65110}
0 commit comments