• ios使用http来上传图片实现方法


    if (parameters) {
            
            int genderNumber = 1;
            self.token = loginToken;
            self.personPK = kidPK;
            self.personName = personNameL;
            self.personNickName = nickNameL;
            self.gender = genderL;
            self.birthday = birthdayL;
            self.imageData = imageL;
            self.imageName = imageNameL;
            if ([self.gender isEqualToString:@"女"]) {
                
                genderNumber = 2;
            }
            
            [parameters setValue:self.token forKey:@"token"];
            [parameters setValue:self.personPK forKey:@"personPk"];
            [parameters setValue:self.personName forKey:@"person.name"];
            [parameters setValue:self.personNickName forKey:@"person.nickname"];
            [parameters setValue:[NSString stringWithFormat:@"%d", genderNumber] forKey:@"person.gender"];
            [parameters setValue:self.birthday forKey:@"birthday"];
            [parameters setValue:self.personNickName forKey:@"turFileFileName"];
            //[parameters setValue:image forKey:@"turFile"];
            
            
        }
        
        self.LoadStatus = LOADING;
        self.strURL = tmp;
        NSLog(@"the url is %@",self.strURL);
        self.activeDownload = [NSMutableData data];
        
        
        //分界线的标识符
        NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
        //根据url初始化request
        NSMutableURLRequest* requestL = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.strURL]
                                                                cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                            timeoutInterval:30];
        //分界线 --AaB03x
        NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
        //结束符 AaB03x--
        NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];
        //得到图片的data
        // self.imageData = UIImagePNGRepresentation(image);
        //http body的字符串
        NSMutableString *body=[[NSMutableString alloc]init];
        //参数的集合的所有key的集合
        NSArray *keys= [parameters allKeys];
        
        //遍历keys  注意这个除去了data类型只是包含了string类型的
        for(int i=0;i<[keys count];i++)
        {
            //得到当前key
            NSString *key=[keys objectAtIndex:i];
            //如果key不是pic,说明value是字符类型,比如name:Boris
            //添加分界线,换行
            [body appendFormat:@"%@ ",MPboundary];
            //添加字段名称,换2行
            [body appendFormat:@"Content-Disposition: form-data; name="%@" ",key];
            //添加字段的值
            [body appendFormat:@"%@ ",[parameters objectForKey:key]];
            
        }

        //注意这里的turFile其实就是要上传的file的字段名字
        ////添加分界线,换行
        [body appendFormat:@"%@ ",MPboundary];
        //声明pic字段,文件名为boris.png
        [body appendFormat:@"Content-Disposition: form-data; name="turFile"; filename="boris.jpg" "];
        //声明上传文件的格式
        [body appendFormat:@"Content-Type: image/jpeg "]; //这里因为之前压缩生成的是jpg类型的图片所以需要表明jpeg,如果是png就是png
        
        //声明结束符:--AaB03x--
        NSString *end=[[NSString alloc]initWithFormat:@" %@",endMPboundary];
        //声明myRequestData,用来放入http body
        NSMutableData *myRequestData=[NSMutableData data];
        //将body字符串转化为UTF8格式的二进制
        [myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
        //将image的data加入
        [myRequestData appendData:self.imageData];
        //加入结束符--AaB03x--
        [myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
        
        //设置HTTPHeader中Content-Type的值
        NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];
        //设置HTTPHeader
        [requestL setValue:content forHTTPHeaderField:@"Content-Type"];
        //设置Content-Length
        [requestL setValue:[NSString stringWithFormat:@"%d", [myRequestData length]] forHTTPHeaderField:@"Content-Length"];
        //设置http body
        [requestL setHTTPBody:myRequestData];
        //http method
        [requestL setHTTPMethod:@"POST"];
        
        //建立连接,设置代理
        NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:requestL delegate:self];
        self.loadConnection = conn;
        [conn release];
        
        if (self.loadConnection != nil)
        {
            [self.loadConnection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
        }

    压缩的方法实现:

    -(void)compraseImage
    {
        UIImage *largeImage = self.pictureViewUpload.image;
        
        double compressionRatio = 1;
        int resizeAttempts = 5;
        
        NSData * imgData = UIImageJPEGRepresentation(largeImage,compressionRatio);
        
        //NSLog(@"Starting Size: %i", [imgData length]);
        
        //Trying to push it below around about 0.4 meg
        while ([imgData length] > 400000 && resizeAttempts > 0) {
            resizeAttempts -= 1;
            
            //NSLog(@"Image was bigger than 400000 Bytes. Resizing.");
            //NSLog(@"%i Attempts Remaining",resizeAttempts);
            
            //Increase the compression amount
            compressionRatio = compressionRatio*0.5;
            //NSLog(@"compressionRatio %f",compressionRatio);
            //Test size before compression
            //NSLog(@"Current Size: %i",[imgData length]);
            imgData = UIImageJPEGRepresentation(largeImage,compressionRatio);
            
            //Test size after compression
            //NSLog(@"New Size: %i",[imgData length]);
        }
        
        //Set image by comprssed version
        //self.pictureView.image = [UIImage imageWithData:imgData];
        
        //Check how big the image is now its been compressed and put into the UIImageView
        
        // *** I made Change here, you were again storing it with Highest Resolution ***
        NSData *endData = UIImageJPEGRepresentation(largeImage,compressionRatio);
        //NSLog(@"Ending Size: %i", [endData length]);
        
        /* NSString *path = [self createPath:@"myImage.jpg"];
         NSLog(@"%@",path);
         [endData writeToFile:path atomically:YES];*/
        
        [self startUploadingRequestWithData:endData andImage:self.pictureViewUpload.image];
    }

  • 相关阅读:
    linux的lsof命令
    linux find
    linux 查看磁盘空间大小
    eclipse运行时编码设置
    WebService工作原理
    Java获取字符串编码方式
    JavaScript,base64加密解密
    如何用javascript 的eval动态执行一个需要传对象参数的函数
    struts2结果类型
    执行maven-build.cmd失败
  • 原文地址:https://www.cnblogs.com/lisa090818/p/3895498.html
Copyright © 2020-2023  润新知