2015-12-03 126 views
1

我想用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?

回答

1

即使它不是JSON,也已指定AFJSONResponseSerializer。當然,你已經重寫了acceptableContentTypes,但是這並不能阻止它在響應中試圖解析JSON。我想用AFHTTPResponseSerializer,然後丟失acceptableContentTypes

順便說一句,我不會使用image/jpeg作爲base64編碼的響應,因爲它是文本而不是jpeg。如果你要返回一個原始的base64字符串,你可以使用application/text或類似的東西。

或者,更好的辦法是改變你的PHP實際返回JSON(因爲這樣可以更容易解析響應)並保持AFJSONResponseSerializer(但是一旦你修復了頭文件就會丟失acceptableContentTypes),然後你可以抓住base64字符串從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)); 

?> 

或者,使用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; 

?> 
+0

謝謝你現在想你的第二個建議...不知道如何實現您在iOS端的第一個建議。 – ewizard

+1

真正的JSON方法?然後你可以用'response [「image」]''獲取base64字符串,然後對其進行解碼。 – Rob

+0

我改變了PHP到你的建議......我將更新我的iOS代碼現在看起來像...我也得到一個新的錯誤,但我認爲我現在進一步。謝謝你的幫助! – ewizard

相關問題