标签:
第一步遵守协议
//UIImagePickerControllerDelegate调用当前设备的相机功能
//UINavigationControllerDelegate一同使用,才能调用相机功能成功(拍照,录像,相册)
UIImagePickerControllerDelegate,UINavigationControllerDelegate
第二步导入第三方库#import "AFHTTPRequestOperationManager.h"
//调用相机类
UIImagePickerController * picker;
第三步:单击ActionSheet按钮调用该方法
//点击ActionSheet按钮跳转到相册页面 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 3) { //调用设备相册 //<1>创建设备相机对象 picker = [[UIImagePickerController alloc]init]; //<2>设置相机资源的样式 /* UIImagePickerControllerSourceTypePhotoLibrary, 图片库 UIImagePickerControllerSourceTypeCamera, 相机 UIImagePickerControllerSourceTypeSavedPhotosAlbum 相册 */ picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //<3>设置picker的代理 picker.delegate = self; //<4>跳转到相册页面 [self presentViewController:picker animated:YES completion:nil]; } }
第四步:(选中相册中的照片时)调用该方法
//选中图片就会调用以下方法----------UIImagePickControllerDelegate协议中的方法 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { //<1>获取选中的图片 UIImage * selectImage = [info objectForKey:UIImagePickerControllerOriginalImage]; //图片上传传递是图片的NSData数据 //<2>将UIImage 转成NSNata //真正开发中图片都是png图片 NSData * imageData = UIImagePNGRepresentation(selectImage); //拓展 //NSData imageJPGData = UIImageJPEGRepresentation(转换图片的对象原始指针, 图片的缩放比例); //<3>上传图片 AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; //封装请求体(也就是参数和参数值) NSMutableDictionary * dic = [[NSMutableDictionary alloc]init]; [dic setObject:self.album_token forKey:@"m_auth"]; [dic setObject:[NSNumber numberWithInt:0] forKey:@"albumid"]; [dic setObject:@"image" forKey:@"pic_title"]; [manager POST:UPLOADPATH parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { //对请求体进行拼接 /* 1、图片的NSData类型的对象指针 2、接口的参数名 3、图片的名称 随意 图片名称不能是中文 后缀如果是jepg也可以写png 4、typt 值固定, 只要是图片就这么写 */ [formData appendPartWithFileData:imageData name:@"attach" fileName:@"hello.png" mimeType:@"image/png"]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSString * message = [responseObject objectForKey:@"message"]; NSLog(@"%@",message); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error:%@",error.description); }]; [self dismissViewControllerAnimated:YES completion:nil]; }
最后上传成功。
标签:
原文地址:http://www.cnblogs.com/huoxingdeguoguo/p/4607298.html