从iOS

时间:2015-07-10 23:26:15

标签: php mysql objective-c afnetworking-2

我一直在网上寻找一种方法,通过PHP使用AFNetworking 2.0将图像从iOS应用程序上传到mySQL数据库。我在搜索和搜索后终于找到了解决方案。我可以拍照并将其上传到我的数据库。当我下载图像时,它保存为“Locations-29.image”或“Locations-30.image”我只是不明白为什么文件扩展名为.image?当图像保存到数据库时,它显示如下:/ var / lib / php / session / phpA7fFDB

我真的很感激任何帮助。它与Xcode有关吗?我的问题是如何将其保存为.jpeg / png文件而不是我现在得到的文件? URL可以是/ var / lib / php / session / phpA7fFDB吗?

这是我的代码:

- (IBAction)postImageToServer:(UIButton*)sender {

CGFloat compression = 1.0f;
UIImage *image = _uploadImage.image;

NSData *imageData1 = UIImageJPEGRepresentation(image, compression);   
int random = arc4random() % 9999999;
NSString *photoName=[NSString stringWithFormat:@"%d-image.jpg",random];

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]     initWithBaseURL:[NSURL URLWithString:@"http://urlhere.com"]];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSDictionary *parameters = @{@"fileName": photoName};
AFHTTPRequestOperation *op = [manager POST:@"getImage.php?" parameters:parameters     constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData1 name:@".jpg" fileName:photoName     mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
[op start];
}

这是我的PHP文件:

<?php
$hostusername = "";
$hostpassword = "";
$hostname = ""; 

$name = $_POST['name'];
$image1 = basename($_FILES['image']['tmp_name']);
$tmp_img = $_FILES['image']['tmp_name'];

$dbhandle = mysql_connect($hostname, $hostusername, $hostpassword) 
 or die("Unable to connect to MySQL");

$selected = mysql_select_db("em",$dbhandle) 
or die("Could not select userName");

$query = mysql_query("INSERT INTO Locations (name, image)
        VALUES ('$name','$tmp_img')");

mysql_close($dbhandle);
?>

0 个答案:

没有答案