将图像从iOS上传到PHP web服务器
问题描述:
我正在开发一个iOS应用程序,用于将图像(从前置摄像头或图库中获取)上传到网络服务器。我不知道如何从iOS发送该图像以及如何使用PHP检索图像。请帮助。将图像从iOS上传到PHP web服务器
答
您可以使用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];
可以转换为'NSData'在BLOB字段在MySQL –
@NiravGadhiya后保存在数据库表中的图像是的,我有同样的想法,但我找不到有用的源代码。 –
我后来发布了一个解决方案。 http://stackoverflow.com/questions/1266176/upload-file-to-ftp-server-on-iphone/1272889#1272889 – Dutchie432