IOS网络笔记--图片上传服务器Demo

申明:此为本人学习笔记,若有纰漏错误之处的可留言共同探讨


/*

 文件上传思路

 1.界面搭建 (两个按钮 一个ImageView 具体步骤略)

 2. 一个选择按钮的点击方法创建 UIActionSheet (记得遵循协议)

 3.实现UIActionSheet协议方法-(void)...clickedButtonAtIndex

 4.在clickedButtonAtIndex内创建imagePickerController  设置  sourceType allowEditing  委托 遵循协议 VC页面跳转

 5.实现UIImagePickerController协议方法

 -(void)...didFinishPickingMediaWithInfo     // JSON解析接收info传过来的数据,用一开创建的ImageView接受 跳回上个界面

 -(void)imagePickerControllerDidCancel 点击取消(返回上一页)

 6.在上传按钮的点击方法 发送post请求

 */


附上Demo和服务器UploadServer : http://yunpan.cn/cFVedkTB4qY6u (提取码:65cf)

( 服务器的修改路径的方法配图在本文最后)

#pragma mark - 选中图片的点击方法

-(void)selectePicClick

{

    NSLog(@"select");

    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"选择要上传的图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"相册图片"otherButtonTitles:@"拍照上传", nil];

    [actionSheet showInView:self.view];

    

}


#pragma mark  上传图片的点击方法

-(void)upLoadClick

{

    NSLog(@"upload");

    NSString* urlString = @"http://127.0.0.1:8080/UploadServer/NewServlet";

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];

    

    // 转成data数据

    NSData *data =UIImageJPEGRepresentation(imageView.image, 1);


    // 数据body

    [request setHTTPBody:data];

    [request setHTTPMethod:@"post"];

    

    // 发送请求

    [NSURLConnection connectionWithRequest:request delegate:nil];

}


#pragma mark - 协议方法

// 点击actionSheet方法

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    NSLog(@"sheetClick");

    // 创建imagePickerController  设置  sourceType allowEditing  委托 遵循协议 VC页面跳转

    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];

    imagePickerController.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;

    imagePickerController.allowsEditing = YES;

    imagePickerController.delegate = self;

    [self presentViewController:imagePickerController animated:YES completion:nil];

    

}


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    NSLog(@"imageInfo %@",info);

    

    // 接收info传过来的数据,用一开创建的ImageView接受

    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];

    imageView.image = image;

    [picker dismissViewControllerAnimated:YES completion:nil];

}


-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    // 点击取消方法

    NSLog(@"cancel");

    [picker dismissViewControllerAnimated:YES completion:nil];

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值