iOS-上传图片

作者: Joh蜗牛 | 来源:发表于2018-03-29 11:28 被阅读789次
    
    // 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:@"上传头像失败,请重新上传"];
        }];
    
    

    相关文章

      网友评论

        本文标题:iOS-上传图片

        本文链接:https://www.haomeiwen.com/subject/nwkmcftx.html