iOS 技术分享

iOS-上传图片

2018-03-29  本文已影响789人  Joh蜗牛

// 1.请求管理者
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Index/userphoto/",SERVERURL];
    // 2.拼接请求参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"mobile"] = self.userMobileLbl.text;
    // 3.发送请求
    [mgr POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        // 拼接文件数据
        NSData *data = UIImageJPEGRepresentation(localImg, 1.0);
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
         // 设置时间格式
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
        [formData appendPartWithFileData:data name:@"img1" fileName:fileName mimeType:@"image/jpg"];
    } success:^(AFHTTPRequestOperation *operation, NSDictionary *json) {
//        LXLog(@"%@", json);
        [MBProgressHUD hideHUD];
        [json[@"status"] isEqual: @"1"] ? [MBProgressHUD showSuccess:@"上传成功!"]:[MBProgressHUD showError:@"上传失败!"];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD showError:@"上传头像失败,请重新上传"];
    }];

上一篇下一篇

猜你喜欢

热点阅读