七牛上傳圖片問題總結


1.圖片的不同狀態顯示

解決方法一:

本地路徑 網絡路徑 狀態
1 0 點擊上傳
1 1 已上傳
0 1 已上傳
0 0 點擊拍照

if([image hasPrefix:@"http"] ) {
      //只要有網絡路徑或者本地圖片已上傳 --  就代表已上傳
    [cell.uploadButton setTitle:@"已上傳" forState:UIControlStateNormal];
    cell.UploadStausImageView.image = [UIImage imageNamed:@"uploadedMany"];
}else if ([path hasPrefix:@"/"] ) { //本地有  但是沒有網絡路徑  --代表 等待上傳
    [cell.uploadButton setTitle:@"點擊上傳" forState:UIControlStateNormal];
    cell.UploadStausImageView.image = [UIImage imageNamed:@"waitingUpload"];
}else {
     [cell.uploadButton setTitle:@"點擊拍照" forState:UIControlStateNormal];
    cell.UploadStausImageView.image = [UIImage imageNamed:@"clickToTakePhoto"];
 }

這只是剛入坑而已.這樣做存在一個問題:本地路徑,網絡路徑同時存在的話有兩種可能 1.已上傳 2.點擊上傳(更新圖片的情況)

於是又有了以下思路:利用標記的思想,來判斷本地圖片是否已經上傳過.

標記本地圖片 狀態
0 點擊上傳
1 已上傳
if([image hasPrefix:@"http"] ) { //只要有網絡路徑或者本地圖片已上傳 --  就代表已上傳
    //既有網絡路徑又有本地路徑   有兩種情況
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:path] isEqualToString:@"0"] ) {
          [cell.uploadButton setTitle:@"點擊上傳" 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"]) { //本地有  但是沒有網絡路徑  --代表 等待上傳
    [cell.uploadButton setTitle:@"點擊上傳" forState:UIControlStateNormal];
    cell.UploadStausImageView.image = [UIImage imageNamed:@"waitingUpload"];
}else {
    [cell.uploadButton setTitle:@"點擊拍照" forState:UIControlStateNormal];
    cell.UploadStausImageView.image = [UIImage imageNamed:@"clickToTakePhoto"];
}

2.上傳單張圖片

公司利用的是一個第三方(七牛),自己也封裝了一個上傳單張圖片的類

- (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.上傳多張圖片

七牛第三方並沒有提供多張圖片上傳的方法,但是無所謂.

//上傳多張圖片
- (void)uploadImagesWithFileArray:(NSMutableArray *)fileArray progress:(void(^)(CGFloat))progress success:(void(^)(NSString *))success failure:(void(^)())failure {
//判斷網絡狀態
AFNetworkReachabilityManager *netStatus = [AFNetworkReachabilityManager sharedManager];
[netStatus startMonitoring];
[netStatus setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    if (status == AFNetworkReachabilityStatusNotReachable) {//無網絡連接
        [SVProgressHUD showWithStatus:@"當前無網絡連接"];
    }
    if (status == AFNetworkReachabilityStatusReachableViaWWAN) { //手機自帶網絡
        //提示用戶是否繼續進行上傳圖片
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"當前使用的手機流量,您是否繼續?" 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) { //未知網絡
    }
}];

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM