【问题标题】:save image to mysql database base64 encode/decode ios将图像保存到mysql数据库base64编码/解码ios
【发布时间】:2015-04-08 08:48:00
【问题描述】:

我正在尝试将图像作为字符串保存到我的数据库中。 这就是我将其编码为 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 时,我得到空值。我做错了什么?

【问题讨论】:

    标签: ios image base64 decode encode


    【解决方案1】:

    已解决:

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

    【讨论】:

      猜你喜欢
      • 2016-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      • 2013-10-25
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      相关资源
      最近更新 更多