iOS怎样将相册的图片上传到网上

来源:互联网 发布:二级建造师网络课程 编辑:程序博客网 时间:2024/04/29 23:27

#import "ViewController.h"


@interface ViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,NSURLConnectionDelegate>


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

//在故事版创建了一个选择照片的按键

- (IBAction)choosePicture:(id)sender

{

//    创建一个actionSheet,在此之前添加代理

    UIActionSheet*actionSheet=[[UIActionSheetalloc]initWithTitle:@"选择相片来源"delegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"从相册中选择",@"从相机拍摄",nil];

    [actionSheetshowInView:self.view];

}

//实现代理方法

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

{

//    做一个相片选择器,只要点击了任意按键就会进入相片选择器

    UIImagePickerController*imagePicker=[[UIImagePickerControlleralloc]init];

//    相片的来源是什么

    imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;

//设置代理

//    imagePicker.allowsEditing=YES;

    imagePicker.delegate=self;

//    跳转到相片选择器

    [selfpresentViewController:imagePicker animated:YEScompletion:nil];

}

//实现选择器代理方法

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

{

//    打印图片参数

   NSLog(@"选中了图片%@",info);

//    通过键获取相片;

    UIImage*image= [infoobjectForKey:UIImagePickerControllerEditedImage];

//    xib设置在起始页面的_imageView给他添加图片

   _imageView.image=image;

//    返回起始页面

    [picker dismissViewControllerAnimated:YEScompletion:nil];

}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

//    当点击取消时,返回

    [picker dismissViewControllerAnimated:YEScompletion:nil];

}

//实现上传按键的方法

- (IBAction)upLoad:(id)sender {

//***    上传数据用post请求    ***

//    NSString*str=@"http://192.168.1.163:8080/UploadServer/NewServlet";

//    创建本地服务器的URL

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

   NSURL*url=[NSURLURLWithString:str];

//    创建可变请求

    NSMutableURLRequest*request=[NSMutableURLRequestrequestWithURL:url];

//    创建请求数据

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

//    NSString*str1=[[NSBundle mainBundle] pathForResource:@"psb-1" ofType:@"jpeg"];

//    NSData*data=[[NSData alloc]initWithContentsOfFile:str1];

//    小文件上传

    [requestsetHTTPBody:data];

    [requestsetHTTPMethod:@"post"];

//    大文件上传

//    NSInputStream*stream=[[NSInputStream alloc]initWithFileAtPath:[[NSBundle mainBundle] pathForResource:@"" ofType:@""]];

//响应请求

    [NSURLConnectionconnectionWithRequest:request delegate:self];

}

0 0
原创粉丝点击