Это мое решение для моей проблемы
Код Objective-C:
NSString *email = self.fileController.user.email;
NSString *password = self.fileController.user.password;
UIImage *image = [[UIImage alloc] initWithData:contact.image];
if(image == nil){
//UNKNOWN IMAGE
//image = [UIImage imageNamed:@"unknown.png"];
return;
}
//The $_POST parameters you want to add
NSDictionary *params = @{ @"email": email, @"contact": contact.userid, @"password": password };
//The image to data
NSData *imageData = UIImagePNGRepresentation(image);
//your url
NSString *urlString = @"http://spark-app.freeiz.com/addImage.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"0xKhTmLbOuNdArY";
NSString *kNewLine = @"\r\n";
// Note that setValue is used so as to override any existing Content-Type header.
// addValue appends to the Content-Type header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
// Add the parameters from the dictionary to the request body
for (NSString *name in params.allKeys) {
NSData *value = [[NSString stringWithFormat:@"%@", params[name]] dataUsingEncoding:NSUTF8StringEncoding];
[body appendData:[[NSString stringWithFormat:@"--%@%@", boundary, kNewLine] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"", name] dataUsingEncoding:NSUTF8StringEncoding]];
// For simple data types, such as text or numbers, there's no need to set the content type
[body appendData:[[NSString stringWithFormat:@"%@%@", kNewLine, kNewLine] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:value];
[body appendData:[kNewLine dataUsingEncoding:NSUTF8StringEncoding]];
}
// Add the image to the request body
[body appendData:[[NSString stringWithFormat:@"--%@%@", boundary, kNewLine] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"myPngFile.png\"%@", @"image", kNewLine] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: image/png"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@%@", kNewLine, kNewLine] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[kNewLine dataUsingEncoding:NSUTF8StringEncoding]];
// Add the terminating boundary marker to signal that we're at the end of the request body
[body appendData:[[NSString stringWithFormat:@"--%@--", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
//Now start the request (asynchronous or synchronous)
Код PHP: // изменить к пути должно быть $ uploadfile = "путь к новому изображению". ".png | .jpg | ...";
// selected and uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {
// Temporary file name stored on the server
if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
} else {
print "No image selected/uploaded";
}
с использованием стандартного html (5) будет работать на всех основных баузерах и iOS. Единственное, что может быть конкретным - это css, чтобы хорошо отображать вещи на мобильном устройстве (так же отзывчивое) см. Здесь http://www.codepool.biz/tech-frontier/html5/take-a-photo-and- upload-it-on-mobile-phones-with-html5.html с некоторыми дополнительными, такими как , который позволяет загружать непосредственно с камеры телефона –
Да, но я хочу сделать это в приложении, я добавлю его на вопрос –
запустите приложение в html;) что должно делать приложение? Приложения iOS имеют полный доступ к протоколу сокетов/http, но делают вещи более сложными, чем простой html. –