将图像保存到mysql数据库base64编码/解码ios

时间:2015-04-08 08:48:00

标签: ios image base64 decode encode

我正在尝试将图像作为字符串保存到我的数据库中。 这是我如何将其编码为base64并将其发布到php文件。

UIImage *yourImage= [UIImage imageNamed:@"noimage"];
NSData *imageData = UIImagePNGRepresentation(yourImage);

NSString *string;

if ([imageData respondsToSelector:@selector(base64EncodedStringWithOptions:)]) {
    NSLog(@"ios 7+");
    string = [imageData base64EncodedStringWithOptions:kNilOptions];  // iOS 7+
} else {
    string = [imageData base64Encoding];                              // pre iOS7
}

@try {

    NSString *post =[[NSString alloc] initWithFormat:@"id=%d&person_id=%@&image_string=%@",id,[person getpersonId],string];
    NSLog(@"PostData: %@",post);

    NSURL *url=[NSURL URLWithString:@"http://"];

    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    [request setHTTPBody:postData];


    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

这就是我如何找回它。

   @try {

    NSString *post =[[NSString alloc] initWithFormat:@"id=%d&person_id=%@",id,[person getpersonId]];
    NSLog(@"PostData: %@",post);

    NSURL *url=[NSURL URLWithString:@"http:"];

    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    [request setHTTPBody:postData];

    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSLog(@"Response code: %ld", (long)[response statusCode]);
    if ([response statusCode] >=200 && [response statusCode] <300)
    {
        NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
        NSLog(@"Response ==> %@", responseData);

        NSData *data;

        if ([NSData instancesRespondToSelector:@selector(initWithBase64EncodedString:options:)]) {
            data = [[NSData alloc] initWithBase64EncodedString:responseData options:kNilOptions];  // iOS 7+
            NSLog(@"data ios 7 %@",data);
        } else {
            data = [[NSData alloc] initWithBase64Encoding:responseData];                           // pre iOS7
        }
        NSLog(@"DATA%@",data);
        UIImage *profImag = [UIImage imageWithData:data];
        NSLog(@"%@",profImag);
        [profileImage setImage:profImag];

当我将它从字符串转换为NSData时,我得到null。我做错了什么?

1 个答案:

答案 0 :(得分:0)

解决:

post = [post stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];