0
ich mit diesem Code ein Bild an einen Server HTTP senden:UIImage zu HTTP-Server Objective C senden
-(NSDictionary*)sendImage:(UIImage*)image withDescription:(NSString*)description andTarget:(NSString*)target andTargetId:(int)targetId {
__block NSDictionary *result = nil;
NSData *imageData = UIImagePNGRepresentation(image);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", PREFIX_URL_REQUEST, SEND_IMAGE]]];
[request setHTTPMethod:REQUEST_TYPE_POST];
NSString *boundary = [[NSString alloc] init];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@.%d\"rn", target, targetId] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/%@.png\r\n\r\n", [NSString stringWithFormat:@"%@_%d", target, targetId]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
result = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", result.description);
dispatch_semaphore_signal(sem);
}];
[dataTask resume];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
return result;
}
dies der Server-Code ist:
session_start();
if (isset($_SESSION['username'])) {
$params = explode(".", basename($_FILES["file"]["name"]));
$target = array_values($params)[0];
$id = array_values($params)[1];
if (($target == "profile") || ($target == "question" && $_SESSION["waitingImagesQuestion"] > 0) || ($target == "answer" && $_SESSION["waitingImagesAnswer"] > 0)) {
$imageDirectory = "images/".round(microtime(true)*1000).".png";
if (move_uploaded_file($_FILES["file"]["tmp_name"], $imageDirectory)) {
switch ($target) {
case "profile":
print JsonBuilder::putRecordProfileImage($imageDirectory);
return;
case "answer":
$_SESSION["waitingImagesAnswer"] --;
print JsonBuilder::putRecordAnswerImage($imageDirectory, $id);
return;
case "question":
$_SESSION["waitingImagesQuestion"] --;
print JsonBuilder::putRecordQuestionImage($imageDirectory, $id);
return;
}
}
}
}
print JsonBuilder::getStringError();
funktioniert, aber nur wenn ich die Client-Methode so anrufe ([UIImage imageNamed:@"aaa.png"]
):
[sender sendImage:[UIImage imageNamed:@"aaa.png"] withDescription:@"" andTarget:@"profile" andTargetId:1];
wenn ich die Methode mit einem UIImage nennen (zum Beispiel ein Foto von der Kamerarolle genommen), so:
-(void)sendImage:(UIImage*)image {
return [sender sendImage:image withDescription:@"" andTarget:@"profile" andTargetId:1];
}
funktioniert nicht! Warum?
Dank
die Frage ganz abstrakt ist. zusätzliche Informationen sind erforderlich. erzeugt die Xcode-Debug-Konsole irgendeine Ausgabe? Produziert das Server-Fehlerprotokoll eine Ausgabe? Zuerst müssen Sie verstehen, ob die http-Anfrage überhaupt ausgeführt wird oder nicht. – heximal
funktioniert der Code, aber nur wenn ich der Methode (um das Bild zu senden) ein Bild so gebe: [UIImage imageNamed: @ "aaa.png"], wenn ich der Methode ein UIImage gebe, das von der Kamerarolle abgerufen wird, nicht Arbeit! @heximal –
Sind Sie sicher, Bild ist nicht Null in diesem Moment? – heximal