2013-06-25 119 views
1

我正在开发一个iOS应用程序,用于将图像(从前置摄像头或图库中获取)上传到网络服务器。我不知道如何从iOS发送该图像以及如何使用PHP检索图像。请帮助。将图像从iOS上传到PHP web服务器

+0

可以转换为'NSData'在BLOB字段在MySQL –

+0

@NiravGadhiya后保存在数据库表中的图像是的,我有同样的想法,但我找不到有用的源代码。 –

+1

我后来发布了一个解决方案。 http://stackoverflow.com/questions/1266176/upload-file-to-ftp-server-on-iphone/1272889#1272889 – Dutchie432

回答

7

您可以使用AFNetworking框架(https://github.com/AFNetworking/AFNetworking),这将节省大量的时间,你也有一些例子,说明如何上传图像和处理响应。

这里是上面提到的从源文件上传的例子:

NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"]; 
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5); 
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 
    [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"]; 
}]; 

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { 
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite); 
}]; 
[httpClient enqueueHTTPRequestOperation:operation]; 
+0

好的答案,但。 – Dutchie432

+1

您的回答很有帮助,但我不想使用AFNetworking。 –

+1

你能编辑AFNetworking 2.0的答案吗? –