ewizard
ewizard

Reputation: 2862

Converting responseObject base64 encoded string to UIImage

I am trying to send an image with PHP (on server side) to my iOS app, so that I can display it in a UIImageView.

My server side code is:

<?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;

?>

I receive the image with this code in my iOS app:

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);
   }];

When I run the code - it hits the failure block with an error that reads as the base64 encoded "string" (image) that I am sending:

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.}

What am I doing incorrectly?

Upvotes: 1

Views: 147

Answers (1)

Rob
Rob

Reputation: 437622

You've specified AFJSONResponseSerializer even though it's not JSON. Sure, you've overridden acceptableContentTypes, but that doesn't stop it from trying to parse JSON in the response.

I'd use AFHTTPResponseSerializer and then lose the acceptableContentTypes.

By the way, I wouldn't use image/jpeg for a base64-encoded response, because it's text, not a jpeg. If you're going to return a raw base64-string, you might use application/text or something like that.

Or, better, change your PHP to actually return JSON (because that will make it easier to parse the response) and stay with AFJSONResponseSerializer (but lose the acceptableContentTypes once you fix the header) and then you can grab the base64 string from response[@"image"].

<?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));

?>

Or, use AFImageResponseSerializer and change the PHP to return the image:

<?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;

?>

Upvotes: 1

Related Questions