1

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?

1 Answer 1

1

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;

?>
Sign up to request clarification or add additional context in comments.

4 Comments

thank you trying your second suggestion now...wasn't sure how to implement your first suggestion on iOS side.
The true JSON approach? You can then grab the base64 string with response["image"] and then decode that.
i changed php to what you suggested...and i am going to update what my iOS code looks like right now...I am also getting a new error but I think I am further along now. Thanks for your help!
im gonna try your json approach now that i have your explanation thanks - if that doesnt work i will update my question so you can see what is happening...thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.