标签:
————————————————————————————
从相册,相机获取图像
设置代理《UINavigationControllerDelegate, UIImagePickerControllerDelegate》
#pragma mark - 上传相册中的图片
- (void)uploadImageFromAlbum
{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
// 设置图片的来源
// 1.相机
// 2.相册(图片库)
/*
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
};
*/
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// 相机
// ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
ipc.delegate = self;
[self presentViewController:ipc animated:YES completion:nil];
}
#pragma mark 选择图片的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// 0.销毁图片选择控制器
[picker dismissViewControllerAnimated:YES completion:nil];
// 1.得到用户选中的图片
UIImage *image = info[UIImagePickerControllerOriginalImage];
// 2.上传图片
[self uploadImage:image];
}
- (void)uploadImage:(UIImage *)image
{
// 1.创建请求
NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/MJServer/upload"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
// 2.设置(指定)所要上传文件的路径
NSData *data = UIImagePNGRepresentation(image);
[request setData:data withFileName:@"minion.png" andContentType:@"image/png" forKey:@"file"];
// 3.设置其他请求参数
[request setPostValue:@"zhangsan" forKey:@"username"];
// 3.发送请求
[request startAsynchronous];
// 4.监听请求
[request setCompletionBlock:^{
NSLog(@"上传完毕");
}];
}
标签:
原文地址:http://www.cnblogs.com/qq449832375/p/4671834.html