ios 开发iOS学习笔记OSiOS学习

iOS七牛上传图片总结

2016-06-02  本文已影响5469人  Trowa

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) { //未知网络
    }
  }];
}
上一篇下一篇

猜你喜欢

热点阅读