iOS头像上传

上传用户头像

// 选择的头像
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo
{
    
    _headerIconImage.image=image;
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
    // 1.创建一个管理者
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    NSString *yealUrl = @"http://192.168.31.174:8080/learnapp/user/touxiang";
    // 2.发送一个请求
    [mgr POST:yealUrl parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        NSData *fileData = [[NSData alloc]init];
        fileData = UIImageJPEGRepresentation(image, 0.5);
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        // 设置时间格式
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *randStr = [NSString stringWithFormat:@"%d",arc4random()%900+100];
        
        NSString *fileName = [NSString stringWithFormat:@"%@%@.jpg", str,randStr];
        
        [formData appendPartWithFileData:fileData name:@"file" fileName:fileName mimeType:@"image/jpeg"];
        
       
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
             
        NSLog(@"responseObject%@",responseObject);
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"error%@",error);

    }];
        
    
}

 

posted @ 2015-12-23 13:24  脉脉不得语  阅读(378)  评论(0编辑  收藏  举报