我想用PHP(在服務器端)向我的iOS應用程序發送圖像,以便我可以在UIImageView中顯示它。將responseObject base64編碼的字符串轉換爲UIImage
我的服務器端代碼:
<?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編碼「字符串」(圖片):
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.}
我在做什麼inco rrectly?
謝謝你現在想你的第二個建議...不知道如何實現您在iOS端的第一個建議。 – ewizard
真正的JSON方法?然後你可以用'response [「image」]''獲取base64字符串,然後對其進行解碼。 – Rob
我改變了PHP到你的建議......我將更新我的iOS代碼現在看起來像...我也得到一個新的錯誤,但我認爲我現在進一步。謝謝你的幫助! – ewizard