iOS开发有关头像上传的问题

来源:互联网 发布:逻辑门电路实验数据 编辑:程序博客网 时间:2024/05/22 02:22

目前移动端的兴起,许多公司都在致力于开发自己的app 同时希望保存自己的用户信息,这里就会牵扯一个用户头像上传的问题,在这里讨论一下用户头像上传的问题

在这里我们先封装一个上传图片的管理类

管理类的声明文件  .h

@interface RequestPostUploadHelper : NSObject/** *POST 提交 并可以上传图片目前只支持单张 */+ (NSString *)postRequestWithURL: (NSString *)url  // IN                            postParems: (NSMutableDictionary *)postParems // IN 提交参数据集合                            picFilePath: (NSString *)picFilePath  // IN 上传图片路径                            picFileName: (NSString *)picFileName;  // IN 上传图片名称/** * 修发图片大小 */+ (UIImage *) imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize) newSize;/** * 保存图片 */+ (NSString *)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName;/** * 生成GUID */+ (NSString *)generateUuidString;
实现文件 .m文件

@implementation RequestPostUploadHelperstatic NSString * const FORM_FLE_INPUT = @"file";+ (NSString *)postRequestWithURL: (NSString *)url  // IN                      postParems: (NSMutableDictionary *)postParems // IN                     picFilePath: (NSString *)picFilePath  // IN                     picFileName: (NSString *)picFileName;  // IN{           NSString *TWITTERFON_FORM_BOUNDARY = @"0xKhTmLbOuNdArY";    //根据url初始化request    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]                                                           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData                                                       timeoutInterval:10];    //分界线 --AaB03x    NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];    //结束符 AaB03x--    NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];    //得到图片的data    NSData* data;    if(picFilePath){             UIImage *image=[UIImage imageWithContentsOfFile:picFilePath];        //判断图片是不是png格式的文件        if (UIImagePNGRepresentation(image)) {            //返回为png图像。            data = UIImagePNGRepresentation(image);        }else {            //返回为JPEG图像。            data = UIImageJPEGRepresentation(image, 1.0);        }    }    //http body的字符串    NSMutableString *body=[[NSMutableString alloc]init];    //参数的集合的所有key的集合    NSArray *keys= [postParems allKeys];        //遍历keys    for(int i=0;i<[keys count];i++)    {        //得到当前key        NSString *key=[keys objectAtIndex:i];              //添加分界线,换行        [body appendFormat:@"%@\r\n",MPboundary];        //添加字段名称,换2行        [body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];        //添加字段的值        [body appendFormat:@"%@\r\n",[postParems objectForKey:key]];                 NSLog(@"添加字段的值==%@",[postParems objectForKey:key]);    }        if(picFilePath){    ////添加分界线,换行        [body appendFormat:@"%@\r\n",MPboundary];            //声明pic字段,文件名为boris.png        [body appendFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",FORM_FLE_INPUT,picFileName];        //声明上传文件的格式        [body appendFormat:@"Content-Type: image/jpge,image/gif, image/jpeg, image/pjpeg, image/pjpeg\r\n\r\n"];    }        //声明结束符:--AaB03x--    NSString *end=[[NSString alloc]initWithFormat:@"\r\n%@",endMPboundary];    //声明myRequestData,用来放入http body    NSMutableData *myRequestData=[NSMutableData data];    //将body字符串转化为UTF8格式的二进制    [myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];    if(picFilePath){        //将image的data加入        [myRequestData appendData:data];    }    //加入结束符--AaB03x--    [myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];      //设置HTTPHeader中Content-Type的值    NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];    //设置HTTPHeader    [request setValue:content forHTTPHeaderField:@"Content-Type"];    //设置Content-Length    [request setValue:[NSString stringWithFormat:@"%d", [myRequestData length]] forHTTPHeaderField:@"Content-Length"];    //设置http body    [request setHTTPBody:myRequestData];    //http method    [request setHTTPMethod:@"POST"];            NSHTTPURLResponse *urlResponese = nil;    NSError *error = [[NSError alloc]init];    NSData* resultData = [NSURLConnection sendSynchronousRequest:request   returningResponse:&urlResponese error:&error];    NSString* result= [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];    if([urlResponese statusCode] >=200&&[urlResponese statusCode]<300){        NSLog(@"返回结果=====%@",result);        return result;    }    return nil;}/** * 修发图片大小 */+ (UIImage *) imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize) newSize{    newSize.height=image.size.height*(newSize.width/image.size.width);    UIGraphicsBeginImageContext(newSize);    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];    UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return  newImage;}/** * 保存图片 */+ (NSString *)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName{    NSData* imageData;        //判断图片是不是png格式的文件    if (UIImagePNGRepresentation(tempImage)) {        //返回为png图像。        imageData = UIImagePNGRepresentation(tempImage);    }else {        //返回为JPEG图像。        imageData = UIImageJPEGRepresentation(tempImage, 1.0);    }       NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);        NSString* documentsDirectory = [paths objectAtIndex:0];        NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];    NSArray *nameAry=[fullPathToFile componentsSeparatedByString:@"/"];    NSLog(@"===fullPathToFile===%@",fullPathToFile);    NSLog(@"===FileName===%@",[nameAry objectAtIndex:[nameAry count]-1]);        [imageData writeToFile:fullPathToFile atomically:NO];    return fullPathToFile;}/** * 生成GUID */+ (NSString *)generateUuidString{    // create a new UUID which you own    CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);        // create a new CFStringRef (toll-free bridged to NSString)    // that you own    NSString *uuidString = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);        // transfer ownership of the string    // to the autorelease pool    [uuidString autorelease];        // release the UUID    CFRelease(uuid);        return uuidString;}@end
以上一个上传头像的管理类   


接下来 我们就要进行图片上传的操作了

定义一些方法来进行对图片的操作和处理(从相册中获取,从相机中获取,对图片进行缩放处理)

@interface UploadViewController : UIViewController<UIActionSheetDelegate,UIImagePickerControllerDelegate>- (IBAction)onClickUploadPic:(id)sender;- (void) snapImage;//拍照- (void) pickImage;//从相册里找- (UIImage *) imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize) newSize;- (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName;- (IBAction)onPostData:(id)sender;- (NSString *)generateUuidString;@end

NSString *TMP_UPLOAD_IMG_PATH=@"";@implementation UploadViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (IBAction)onClickUploadPic:(id)sender {    UIActionSheet *menu=[[UIActionSheet alloc] initWithTitle:@"上传图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照上传",@"从相册上传", nil];    menu.actionSheetStyle=UIActionSheetStyleBlackTranslucent;    [menu showInView:self.view];    }- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{     NSLog(@"33333333333333");    if(buttonIndex==0){        [self snapImage];        NSLog(@"111111111111");    }else if(buttonIndex==1){        [self pickImage];        NSLog(@"222222222222");    }        [actionSheet release];}//拍照- (void) snapImage{    UIImagePickerController *ipc=[[UIImagePickerController alloc] init];    ipc.sourceType=UIImagePickerControllerSourceTypeCamera;    ipc.delegate=self;    ipc.allowsEditing=NO;    [self presentModalViewController:ipc animated:YES];    }//从相册里找- (void) pickImage{    UIImagePickerController *ipc=[[UIImagePickerController alloc] init];    ipc.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;    ipc.delegate=self;    ipc.allowsEditing=NO;    [self presentModalViewController:ipc animated:YES];}-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *) info{    UIImage *img=[info objectForKey:@"UIImagePickerControllerOriginalImage"];    if(picker.sourceType==UIImagePickerControllerSourceTypeCamera){//        UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);    }    UIImage *newImg=[self imageWithImageSimple:img scaledToSize:CGSizeMake(300, 300)];    [self saveImage:newImg WithName:[NSString stringWithFormat:@"%@%@",[self generateUuidString],@".jpg"]];    [self dismissModalViewControllerAnimated:YES];    [picker release];}-(UIImage *) imageWithImageSimple:(UIImage*) image scaledToSize:(CGSize) newSize{    newSize.height=image.size.height*(newSize.width/image.size.width);    UIGraphicsBeginImageContext(newSize);    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];    UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return  newImage;}- (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName{    NSLog(@"===TMP_UPLOAD_IMG_PATH===%@",TMP_UPLOAD_IMG_PATH);    NSData* imageData = UIImagePNGRepresentation(tempImage);        NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);        NSString* documentsDirectory = [paths objectAtIndex:0];        // Now we get the full path to the file        NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];        // and then we write it out    TMP_UPLOAD_IMG_PATH=fullPathToFile;    NSArray *nameAry=[TMP_UPLOAD_IMG_PATH componentsSeparatedByString:@"/"];    NSLog(@"===new fullPathToFile===%@",fullPathToFile);    NSLog(@"===new FileName===%@",[nameAry objectAtIndex:[nameAry count]-1]);        [imageData writeToFile:fullPathToFile atomically:NO];    }- (IBAction)onPostData:(id)sender {    NSMutableDictionary * dir=[NSMutableDictionary dictionaryWithCapacity:7];    //[dir setValue:@"save" forKey:@"m"];    [dir setValue:@"IOS上传试试" forKey:@"title"];    [dir setValue:@"IOS上传试试" forKey:@"content"];    [dir setValue:@"28" forKey:@"clubUserId"];    [dir setValue:@"1" forKey:@"clubSectionId"];    [dir setValue:@"192.168.0.26" forKey:@"ip"];    [dir setValue:@"asfdfasdfasdfasdfasdfasd=" forKey:@"sid"];    NSString *url=@"http://192.168.0.26:8090/api/club/topicadd.do?m=save";    NSLog(@"=======上传");    if([TMP_UPLOAD_IMG_PATH isEqualToString:@""]){        [RequestPostUploadHelper postRequestWithURL:url postParems:dir picFilePath:nil picFileName:nil];    }else{        NSLog(@"有图标上传");        NSArray *nameAry=[TMP_UPLOAD_IMG_PATH componentsSeparatedByString:@"/"];       [RequestPostUploadHelper postRequestWithURL:url postParems:dir picFilePath:TMP_UPLOAD_IMG_PATH picFileName:[nameAry objectAtIndex:[nameAry count]-1]];;    } }- (NSString *)generateUuidString{    // create a new UUID which you own    CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);        // create a new CFStringRef (toll-free bridged to NSString)    // that you own    NSString *uuidString = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuid);        // transfer ownership of the string    // to the autorelease pool    [uuidString autorelease];        // release the UUID    CFRelease(uuid);        return uuidString;}@end
到这里图片的上传就完成了,有必要说明一下

在管理器内上传头像的post 请求体的头部的标注是必要的,标明我们要进行文件的上传  分隔符以及文件的大小不是必要,但是推荐标着,这样是为了告诉后台我们的文件开始上传了,我们的文件上传结束了,同时也为了前台和后台的交互

在操作图片上传的方法中,对于字典的封装,我是为了测试所以添加好多字段,在实际操作中只要根据后台需求上传后台所需要的字段就行

接下来做出一些关于图片上传的说明:

由于iOS无法通过html表单来上传图片,因此想要上传图片,必须实现http请求,而不能像其他语言那样通过html表单的post就能上传。

上传图片的http post请求的格式是这样的:

?
1
2
3
4
5
6
7
8
9
10
11
12
Content-type: multipart/form-data, boundary=AaB03x
 
--AaB03x
content-disposition: form-data; name="field1"
 
Hello Boris!
--AaB03x
content-disposition: form-data; name="pic"; filename="boris.png"
Content-Type: image/png
 
... contents of boris.png ...
--AaB03x--




第一行是指定了http post请求的编码方式为multipart/form-data(上传文件必须用这个)。
boundary=AaB03x说明了AaB03x为分界线。比如 --AaB03x 就是一个分界线的意思

content-disposition: form-data; name="field1"

Hello Boris!

  这句话声明了请求中的一个字段的名称,如field1  以及字段的值,如Hello Boris!
这里类似form表单中的<input name="field1" type="text" value="Hello Boris!"/>
中间的空行是必须的。

不同的字段之间用分界线分开,分界线需要单独一行,如 --AaB03x--

分界线的下一行,是下一个字段

content-disposition: form-data; name="pic"; filename="boris.png"
Content-Type: image/png

 ... contents of boris.png ...
 --AaB03x--

这里声明了变量pic,也就是我们要传的文件,上传文件的时候需要在后边指定file name:filename="boris.png"
并且需要在下一行指定文件的格式:Content-Type: image/png


 ... contents of boris.png ...  这里是boris.png的二进制内容,如 <89504e47 0d0a1a0a 0000000d 49484452 000000b4 000000b4 08020000 00b2af91 65000020 00494441 5478012c dd79b724 6b7616f6 8c888c88 8c9c8733 55ddb1d5 6a0db486 06218401 ......

在http post请求的结尾,需要有一个分界线,但是是前后都有--的:--AaB03x--

以上的这些格式,是http的规范,每个空行,空格都是必须的。

0 0