-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQiniuPicUPload.m
144 lines (123 loc) · 4.66 KB
/
QiniuPicUPload.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// QiniuPicUPload.m
// OC
//
// Created by swkj_lsy on 17/3/3.
// Copyright © 2017年 thidnet. All rights reserved.
//
#import "QiniuPicUPload.h"
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <QiniuSDK.h>
#import <MBProgressHUD.h>
//typedef void (^CallBackBlock)();
@implementation QiniuPicUPload
{
MBProgressHUD * HUD;
NSMutableArray *_imageDataArray;
QNUploadManager * _upManager;
NSString * _qiNiuToken;
BOOL _cancelUpLoad;
NSDictionary * _uploadImageDic;
BOOL _isSuccess;
}
- (id)initImage:(NSMutableArray *)imageArray QniuToken:(NSString *)qiniutoken QiniuParameters:(NSDictionary *)qiniuParameters {
if (self = [super init]) {
_uploadImageDic = qiniuParameters;
_qiNiuToken = qiniutoken;
_imageDataArray = imageArray;
}
return self;
}
- (void)start{
BOOL isHttps = TRUE;
QNZone * httpsZone = [[QNAutoZone alloc] initWithHttps:isHttps dns:nil];
QNConfiguration *config = [QNConfiguration build:^(QNConfigurationBuilder *builder) {
builder.zone = httpsZone;
}];
_upManager = [[QNUploadManager alloc] initWithConfiguration:config];
[self createHUD];
[self uploadImage:0];
if (_isSuccess) {
NSLog(@"成功2");
}
}
- (void)uploadImage:(int)num {
__block int i = num;
__block BOOL can;
__weak QiniuPicUPload *weakSelf = self;
NSString * key = [self createGUID];
[_upManager putData:_imageDataArray[i] key:key token:_qiNiuToken
complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
// NSLog(@"info的内容:%@", info);
// NSLog(@"resp的内容:%@", resp);
if (!info.error) {
i++;
if (i < _imageDataArray.count) {
// NSLog(@"看看:%d",i);
[self uploadImage:i];
}else{
HUD.hidden = YES;
can = YES;
weakSelf.callBackBlock(YES);
//这里是提示上传成功的提示框,你可以自己分装更改
[self showHudTitle:@"上传成功" toView:[UIApplication sharedApplication].keyWindow];
}
}else{
HUD.hidden = YES;
can = NO;
weakSelf.callBackBlock(NO);
if (_cancelUpLoad) {
[self showHudTitle:@"取消上传" toView:[UIApplication sharedApplication].keyWindow];
}else{
[self showHudTitle:@"上传失败" toView:[UIApplication sharedApplication].keyWindow];
}
_cancelUpLoad = NO;
}
} option:[self createProgress:i]];
if (can) {
NSLog(@"成功1");
}
_isSuccess = can;
}
-( QNUploadOption *)createProgress:(int)num{
QNUploadOption * ImageOpt = [[QNUploadOption alloc] initWithMime:nil progressHandler:^(NSString *key, float percent) {
dispatch_async(dispatch_get_main_queue(), ^{
HUD.progress = percent/_imageDataArray.count+(float)num/_imageDataArray.count;
});
} params:_uploadImageDic checkCrc:NO cancellationSignal:^BOOL{
return _cancelUpLoad;
}];
return ImageOpt;
}
- (void)createHUD{
HUD = [[MBProgressHUD alloc] initWithView:[UIApplication sharedApplication].keyWindow];
[[UIApplication sharedApplication].keyWindow addSubview:HUD];
HUD.mode = MBProgressHUDModeAnnularDeterminate;
HUD.labelText = @"上传";
HUD.labelFont = [UIFont systemFontOfSize:12];
HUD.opacity = 0.9;
HUD.margin = 15;
[HUD show:YES];
}
- (void)showHudTitle:(NSString *)hudString toView:(UIView *)view
{
// 显示文字
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.userInteractionEnabled = NO;
hud.mode = MBProgressHUDModeText;
hud.labelText = NSLocalizedString(hudString, @"HUD message title");
hud.labelFont = [UIFont systemFontOfSize:14];
hud.opacity = 0.7;
hud.margin = 12;
[hud hide:YES afterDelay:1.2];
}
- (NSString *)createGUID{
CFUUIDRef uuid_ref = CFUUIDCreate(NULL);
CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);
CFRelease(uuid_ref);
NSString *uuid = [NSString stringWithString:(__bridge NSString*)uuid_string_ref];
CFRelease(uuid_string_ref);
return uuid;
}
@end