2018-02-06-上传壁纸功能(大佬版)

2018/2/6 posted in  工作总结

上传壁纸

  • 选中一个图片,并且发布
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"上传壁纸";
    // 点击UIButton AddWallpaper 是打开相册添加图片
    [_AddWallpaper addTarget:self action:@selector(AddWallpaperClick) forControlEvents:UIControlEventTouchUpInside];
    
    // 点击UIButton UpWallPaper 是上传图片
    [_UpWallpaper addTarget:self action:@selector(UpWallpaperClick) forControlEvents:UIControlEventTouchUpInside];
}

# pragma mark -- 添加壁纸
- (void)AddWallpaperClick {

    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择获取图片的位置" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *setAlert = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[QWImageTool sharedInstance] ShowTakePhotoWithController:self didFinishPicking:^(UIImage *image) {
            [self uploadImage:image];
            
        }];
    }];
    UIAlertAction *PhoneAlert = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //打开相册
        [[QWImageTool sharedInstance] ShowLocalPhotoWithController:self didFinishPicking:^(UIImage *image) {
            [self uploadImage:image];
            //            [self.headImageButton setImage:image forState:UIControlStateNormal];
            
        }
         ];
        
    }];
    UIAlertAction *hidAlert = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:setAlert];
    [alert addAction:PhoneAlert];
    [alert addAction:hidAlert];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}
- (void)UpWallpaperClick {

    [self uploadImageUrl:self.wallpaperUrl];
}


- (void)uploadImage:(UIImage *)image {

    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:[QWAccountModel sharedModel].uid forKey:@"uid"];
    QWLog(@"uid====%@",[QWAccountModel sharedModel].uid);
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"正在上传壁纸";
    [QWImageTool uploadWithImageArray:@[image] url:[QWHttpUrls uploadImageUrl] filename:@"myfile.jpeg" name:@"myfile" parameters:param  success:^(id  _Nullable json) {
        NSString *status = [NSString stringWithFormat:@"%@",json[@"status"]];
        QWLog(@"json===%@",json);
        QWLog(@"status===%@",status);
        if ([status isEqualToString:@"1"]) {
            NSString *iconPath = [NSString stringWithFormat:@"%@",json[@"data"]];
            _UpImage.image = image;
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [QWShowMessage showMessage:@"上传壁纸成功"];
            self.wallpaperUrl = iconPath;
//            [self uploadImageUrl:iconPath];
        }else{
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            NSString *msg = [NSString stringWithFormat:@"%@",json[@"msg"]];
            if (![QWStringUtils isBlankString:msg]) {
                [QWShowMessage showMessage:msg];
            }else{
                [QWShowMessage showMessage:@"上传壁纸失败"];
            }
        }
    } failure:^(NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        //[QWShowMessage showMessage:@"网络异常"];
    }];
    
}
- (void)uploadImageUrl:(NSString *)url{
    if ([WoWStringUtils isBlankString:url]) {
         [QWShowMessage showMessage:@"上传壁纸失败"];
        return;
    }
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:[QWAccountModel sharedModel].uid forKey:@"uid"];
    [param setValue:url forKey:@"iconPath"];
    [param setValue:@"play" forKey:@"icon_title"];//现在是默认参数,以后有需要修改
    [param setValue:@"1" forKey:@"title_id"];//现在是默认参数,以后有需要修改
    [QWHttpTool postmanager:[QWHttpUrls upPaperUrl] params:param success:^(id  _Nonnull manager, id  _Nonnull json) {
        
        NSString *status = [NSString stringWithFormat:@"%@",json[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [QWShowMessage showMessage:@"上传壁纸成功"];
            self.wallpaperUrl = @"";
            _UpImage.image = nil;
            
        }else{
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            NSString *msg = [NSString stringWithFormat:@"%@",json[@"msg"]];
            if (![QWStringUtils isBlankString:msg]) {
                [QWShowMessage showMessage:msg];
            }else{
                [QWShowMessage showMessage:@"上传壁纸失败"];
            }
        }
    } failure:^(NSError * _Nonnull error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [QWShowMessage showMessage:@"网络异常"];
    }];
    
}
  • 看完这种写法,我发现我前两篇文章写得真的是蠢。
  • 项目里有这样的方法,我也尝试过,但是怎么就是写不出来,我想我还需要更加深造