1.圖片的不同狀態(tài)顯示
解決方法一:
本地路徑 | 網(wǎng)絡(luò)路徑 | 狀態(tài) |
---|---|---|
1 | 0 | 點(diǎn)擊上傳 |
1 | 1 | 已上傳 |
0 | 1 | 已上傳 |
0 | 0 | 點(diǎn)擊拍照 |
if([image hasPrefix:@"http"] ) {
//只要有網(wǎng)絡(luò)路徑或者本地圖片已上傳 -- 就代表已上傳
[cell.uploadButton setTitle:@"已上傳" forState:UIControlStateNormal];
cell.UploadStausImageView.image = [UIImage imageNamed:@"uploadedMany"];
}else if ([path hasPrefix:@"/"] ) { //本地有 但是沒(méi)有網(wǎng)絡(luò)路徑 --代表 等待上傳
[cell.uploadButton setTitle:@"點(diǎn)擊上傳" forState:UIControlStateNormal];
cell.UploadStausImageView.image = [UIImage imageNamed:@"waitingUpload"];
}else {
[cell.uploadButton setTitle:@"點(diǎn)擊拍照" forState:UIControlStateNormal];
cell.UploadStausImageView.image = [UIImage imageNamed:@"clickToTakePhoto"];
}
這只是剛?cè)肟佣?這樣做存在一個(gè)問(wèn)題:本地路徑,網(wǎng)絡(luò)路徑同時(shí)存在的話有兩種可能
- 1.已上傳
- 2.點(diǎn)擊上傳(更新圖片的情況)
于是又有了以下思路:利用標(biāo)記的思想,來(lái)判斷本地圖片是否已經(jīng)上傳過(guò).
標(biāo)記本地圖片 | 狀態(tài) |
---|---|
0 | 點(diǎn)擊上傳 |
1 | 已上傳 |
if([image hasPrefix:@"http"] ) { //只要有網(wǎng)絡(luò)路徑或者本地圖片已上傳 -- 就代表已上傳
//既有網(wǎng)絡(luò)路徑又有本地路徑 有兩種情況
if ([[[NSUserDefaults standardUserDefaults]objectForKey:path] isEqualToString:@"0"] ) {
[cell.uploadButton setTitle:@"點(diǎn)擊上傳" forState:UIControlStateNormal];
cell.UploadStausImageView.image = [UIImage imageNamed:@"waitingUpload"];
}else {
[cell.uploadButton setTitle:@"已上傳" forState:UIControlStateNormal];
cell.UploadStausImageView.image = [UIImage imageNamed:@"uploadedMany"];
}
}else if ([path hasPrefix:@"/"] || [[[NSUserDefaults standardUserDefaults]objectForKey:path] isEqualToString:@"0"]) { //本地有 但是沒(méi)有網(wǎng)絡(luò)路徑 --代表 等待上傳
[cell.uploadButton setTitle:@"點(diǎn)擊上傳" forState:UIControlStateNormal];
cell.UploadStausImageView.image = [UIImage imageNamed:@"waitingUpload"];
}else {
[cell.uploadButton setTitle:@"點(diǎn)擊拍照" forState:UIControlStateNormal];
cell.UploadStausImageView.image = [UIImage imageNamed:@"clickToTakePhoto"];
}
2.上傳單張圖片
公司利用的是一個(gè)第三方(七牛),自己也封裝了一個(gè)上傳單張圖片的類
- (void)uploadWithFile:(NSString *)file withProgress:(QNUpProgressHandler)progress success:(void(^)(NSString*url))success failure:(void(^)())failure {
UIImage *getImage = [UIImage imageWithContentsOfFile:file];
QNUploadOption *option = [[QNUplLBHTTPRequestoadOption alloc] initWithMime:nil progressHandler:progress params:nil checkCrc:NO cancellationSignal:nil];
//先獲取token
[LBHTTPRequest postImage:[LBHTTPRequest getUserId] token:[LBHTTPRequest getUserToken] userType:@"1" fileExt:@"png" SuccessBlock:^(BOOL isSuccess, NSDictionary *resultDic) {
if (isSuccess) {
NSString * token = resultDic[@"uploadToken"];
QNUploadManager *upManager = [[QNUploadManager alloc] init];
NSData *data;
if (UIImagePNGRepresentation(getImage) == nil){
data = UIImageJPEGRepresentation(getImage, 1);
} else {
data = UIImagePNGRepresentation(getImage);
}
[upManager putData:data key:resultDic[@"fileKey"] token:token
complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
[SVProgressHUD dismiss];
// isLoadLogo = NO;
if([resp[@"result"] intValue] == 200){
[LBUploadManager sharedInstance].imageUrl = resp[@"fileUrl"];
success([LBUploadManager sharedInstance].imageUrl);
}
} option:option];
}
}];
}
3.上傳多張圖片
七牛第三方并沒(méi)有提供多張圖片上傳的方法,但是無(wú)所謂.
//上傳多張圖片
- (void)uploadImagesWithFileArray:(NSMutableArray *)fileArray progress:(void(^)(CGFloat))progress success:(void(^)(NSString *))success failure:(void(^)())failure {
//判斷網(wǎng)絡(luò)狀態(tài)
AFNetworkReachabilityManager *netStatus = [AFNetworkReachabilityManager sharedManager];
[netStatus startMonitoring];
[netStatus setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
if (status == AFNetworkReachabilityStatusNotReachable) {//無(wú)網(wǎng)絡(luò)連接
[SVProgressHUD showWithStatus:@"當(dāng)前無(wú)網(wǎng)絡(luò)連接"];
}
if (status == AFNetworkReachabilityStatusReachableViaWWAN) { //手機(jī)自帶網(wǎng)絡(luò)
//提示用戶是否繼續(xù)進(jìn)行上傳圖片
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"當(dāng)前使用的手機(jī)流量,您是否繼續(xù)?" delegate:[LBUploadManager sharedInstance] cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
[alert show];
}
if (status == AFNetworkReachabilityStatusReachableViaWiFi) { //wifi
//1.獲取所有的圖片
NSMutableArray *imageArray = [[LBUploadManager sharedInstance]getImageArrayWithFileArray:fileArray];
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_async(queue, ^{
for (int i = 0; i < imageArray.count; i ++ ) {
dispatch_async(queue, ^{ //上傳頭像
[[LBUploadManager sharedInstance]uploadWithFile:imageArray[i] withProgress:^(NSString *key, float percent) {
progress(percent);
} success:^(NSString *url) {
success(url);
//獲取studentId
NSString *key = [[LBUploadManager sharedInstance].fileArray[i] allKeys].lastObject;
[LBHTTPRequest PostUpdataStudentImageWithStudentId:key withStudentImage:url andSuccessBlock:^(BOOL isSuccess, NSDictionary *resultDic) {
if (isSuccess) {
[[NSNotificationCenter defaultCenter]postNotificationName:@"refresh" object:nil];
[[NSNotificationCenter defaultCenter]postNotificationName:@"updataStudentData" object:nil];
}
[[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:imageArray[i]];
[[LBUploadManager sharedInstance].fileArray removeObject:imageArray[i]];
}];
} failure:^{
}];
});
}
});
}
if (status == AFNetworkReachabilityStatusUnknown) { //未知網(wǎng)絡(luò)
}
}];
}