iOS上传图片到网上,并更新到服务器,常用在设置头像

//封装一个工具类FileUploader

//FileUploader.h

#import <Foundation/Foundation.h>


@interface FileUploader : NSObject

+(void) upLoadImage:(UIImage *) image completionHandler:(void (^)(NSURLResponse* response, NSData* data, NSError* connectionError)) handler;

@end


//FileUploader.m

#import "FileUploader.h"


@implementation FileUploader

+(void) upLoadImage:(UIImage *) image completionHandler:(void (^)(NSURLResponse* response, NSData* data, NSError* connectionError)) handle

{

    

    NSString *path1 = [NSString stringWithFormat:@"http://2.guansir.sinaapp.com/upload.php"];

    NSURL *url = [NSURL URLWithString:path1];

    

    NSData *data = UIImagePNGRepresentation(image);

    

    NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";

    //分界线 --AaB03x

    NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];

    //结束符 AaB03x--

    NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];

    //http body的字符串

    NSMutableString *body=[[NSMutableString alloc]init];

    

    ////添加分界线,换行

    [body appendFormat:@"%@\r\n",MPboundary];

    //声明pic字段,文件名为boris.png

    [body appendFormat:@"Content-Disposition: form-data; name=\"img\"; filename=\"boris.png\"\r\n"];

    //声明上传文件的格式

    [body appendFormat:@"Content-Type: image/png\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]];

    //imagedata加入

    [myRequestData appendData:data];

    //加入结束符--AaB03x--

    [myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];

    

    //设置HTTPHeaderContent-Type的值

    NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];

    

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    //设置HTTPHeader

    [request setValue:content forHTTPHeaderField:@"Content-Type"];

    //设置Content-Length

    [request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[myRequestData length]] forHTTPHeaderField:@"Content-Length"];

    //设置http body

    [request setHTTPBody:myRequestData];

    //http method

    [request setHTTPMethod:@"POST"];

    

    

    

    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        

        handle(response,data,connectionError);

    }];


}

@end

//FileUploader类封装完成========================================================


//使用

    [FileUploader upLoadImage:image completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        if(connectionError==nil)

        {

            if (data != nil) {

                NSLog(@"%@",[[NSString alloc] initWithData:data encoding:4]);

                NSError *error;

                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

                

                if (dic == nil) {

                    

                }else{

                    

                    //如果有值的话将图片和所需要的参数更新到服务端,这里需要的是将图片和老师id传到服务端改教师头像

                    NSString *strImage = [NSString stringWithFormat:@"id=%d&imgHead=%@",self.teachId,dic[@"imageUrl"]];

                    NSLog(@"strImage = %@",strImage);

                    NSData *data1 = [strImage dataUsingEncoding:4];

                    

                    NSString *path = [NSString stringWithFormat:@"%@%@",HOST,TEACHER_UPDATE];

                    NSURL *url = [NSURL URLWithString:path];

                    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

                    [req setHTTPMethod:@"POST"];

                    [req setHTTPBody:data1];

                    [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

                        if (!connectionError) {

                            NSLog(@"更新成功 = %@",[[NSString alloc]initWithData:data encoding:4]);

                        }else

                        {

                            NSLog(@"error = %@",connectionError);

                        }

                    }];

                }

                

            }

        }

        else

        {

            NSLog(@"error = %@",connectionError);

        }

    }];


你可能感兴趣的:(ios,头像上传网络,并更新到服务器)