64位编码后上传图像

时间:2015-04-25 07:35:54

标签: ios objective-c iphone

我想在服务器上上传图片。我编码到64位然后我发布。我正在使用此代码:

NSURL *url=[[NSURL alloc] initWithString:@"http://boomagift.ramansingla.com/userpicture.php?email=sonal@gmail.com"];

NSMutableURLRequest *req=[[NSMutableURLRequest alloc] initWithURL:url];
[req setHTTPMethod:@"POST"];
// [req setValue:@"multipart/form-data; boundary=*****" forHTTPHeaderField:@"Content-Type"];

NSMutableData *postBody=[NSMutableData data];
NSString *boundary=@"*****";


[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSString *this=[pickedImageData base64EncodedString];

[postBody appendData: [this dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];


NSHTTPURLResponse *response=nil;
        NSError *error=[[NSError alloc] init];

NSData *responseData;
    responseData=[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
    NSLog(@"%@",responseData);
    if(responseData&&[responseData length])
    {

        NSDictionary *dictionary=[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
        NSLog(@"%@",dictionary);
    }
    else
    {
        NSLog(@"hello");
    }

1 个答案:

答案 0 :(得分:0)

-(NSArray *)getResizedUImage:(UIImage *)getImage otherValueOne:(float)newWidth otherValueSecond:(float)newHeight
{
    CGRect rect = CGRectMake(0.0, 0.0, newWidth, newHeight);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 1);
    [getImage drawInRect:rect];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSArray *loadImageDateArray = [[NSArray alloc]init];
    UIImage *imageConvert = img;
    NSData *data = UIImageJPEGRepresentation(imageConvert, 0.8);
    NSString *base64String = [data base64EncodedStringWithOptions:0];
    loadImageDateArray = [NSArray arrayWithObject:base64String];
    return loadImageDateArray;
}

此代码部分返回基础64尝试此操作 我更新了我的回答使用这个函数,这个函数返回base 64