将responseObject base64编码的字符串转换为UIImage

时间:2015-12-03 06:27:40

标签: php ios objective-c uiimageview uiimage

我正在尝试使用PHP(在服务器端)将图像发送到我的iOS应用程序,以便我可以在UIImageView中显示它。

我的服务器端代码是:

<?php

header("Content-Type: image/jpeg"); //if your data is format jpeg

$username = $_POST['username'];
$count = $_POST['count'];
$base64string =     base64_encode(file_get_contents("images/".$username."/".$count."/".$username."file".$count.".jpeg"));
echo $base64string;

?>

我在iOS应用中收到带有此代码的图片:

NSString * uploadURL = @"http://192.168.1.4/getimage.php";

NSLog(@"uploadImageURL: %@", uploadURL);

NSString *queryStringss = [NSString stringWithFormat:@"%@", uploadURL];
                queryStringss = [queryStringss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer=[AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"image/jpeg"];

NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];

NSString *usernameEncoded = [marker.title stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSDictionary *params = @{@"username": usernameEncoded, @"count": [object valueForKey:@"count"]};

[manager POST:queryStringss parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
       NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
       NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:responseObject options:0];
       image.image = [UIImage imageWithData:decodedData scale:300/2448];
           [self.view addSubview:image];
       }
   failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       NSLog(@"Error: %@ ***** %@", operation.responseString, error);
   }];

当我运行代码时 - 它会触发故障块,并显示一个错误,该错误读作base64编码&#34;字符串&#34; (图片)我发送的信息:

2015-12-03 01:19:15.655 sneek[6261:1952572] Error: /9j/4AAQSkZJRgABAQAASABIAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAYAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAMwKADAAQAAAABAAAJkAAAAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs+EJ+/8AAEQgJkAzAAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tr

... (very long) ...

Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您已指定AFJSONResponseSerializer,即使它不是JSON。当然,您已覆盖acceptableContentTypes,但这并不能阻止它尝试解析响应中的JSON。

我使用AFHTTPResponseSerializer然后丢失acceptableContentTypes

顺便说一句,我不会将image/jpeg用于base64编码的响应,因为它是文本,而不是jpeg。如果您要返回原始base64字符串,则可以使用application/text或类似内容。

或者,更好的是,将PHP更改为实际返回JSON(因为这样可以更容易地解析响应)并保持AFJSONResponseSerializer(但一旦修复了标题,就会失去acceptableContentTypes)然后你可以从response[@"image"]中获取base64字符串。

<?php

header("Content-Type: application/json");

$username = $_POST['username'];
$count = $_POST['count'];
$base64string =     base64_encode(file_get_contents("images/".$username."/".$count."/".$username."file".$count.".jpeg"));
echo json_encode(array("image" => $base64string));

?>

或者,使用AFImageResponseSerializer并更改PHP以返回图像:

<?php

header("Content-Type: image/jpeg"); //if your data is format jpeg

$username = $_POST['username'];
$count = $_POST['count'];
$contents = file_get_contents("images/".$username."/".$count."/".$username."file".$count.".jpeg"));
echo $contents;

?>