如何将图像文件从保存的 NSUserDefault 上传到 php 服务器?
How can I upload an image file from saved NSUserDefault to php server?
我想在我的应用程序中更改个人资料图片,并在编辑后将图片上传到 PHP 服务器。至此我将更改后的个人资料图片保存到本地。
我的问题是如何将更改后的图像上传到 PHP 服务器(上传图像文件)?我可以使用 AFNetworking 3.X 并且我需要为这种情况编写后端 API 吗?有什么方法可以直接上传图片文件到服务器吗?
到目前为止,这是我使用 UIImagePickerController 所做的,在此阶段之前它工作正常:-
-(void)changeProfileImage:(UITapGestureRecognizer *)recognizer{
if (!_photoManager) {
_photoManager =[[SelectPhotoManager alloc]init];
}
[_photoManager startSelectPhotoWithImageName:@"Select Profile Image"];
__weak typeof(self)mySelf=self;
//Select Photo
_photoManager.successHandle=^(SelectPhotoManager *manager,UIImage *image){
mySelf.imgProfileImage.image = image;
NSData *data = UIImagePNGRepresentation(image);
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"imgProfile"];
};
}
请帮忙。谢谢。
// HTTP method to upload file to web server
-(void)initWithURL:(NSString *)url image:(NSData *)imageData andFileName:(NSString *)filename delegate:(id)del {
// your PHP server url
// your image data
NSString *urlString = url;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
// if access token defined for authrization
NSString *token = [NSString stringWithFormat:@"Bearer %@",[[[CommonUserDefault getUserInfo] objectForKey:@"token"] objectForKey:@"accessToken"]];
[request addValue:token forHTTPHeaderField: @"Authorization"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"files\"; filename=\"%@.jpg\"\r\n",filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
//return and test
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
id json = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];
NSLog(@"json upload picture = %@",json);
int statusCode = [[json objectForKey:@"statusCode"] intValue];
if(statusCode == <errorCode>){
}
}
我想在我的应用程序中更改个人资料图片,并在编辑后将图片上传到 PHP 服务器。至此我将更改后的个人资料图片保存到本地。
我的问题是如何将更改后的图像上传到 PHP 服务器(上传图像文件)?我可以使用 AFNetworking 3.X 并且我需要为这种情况编写后端 API 吗?有什么方法可以直接上传图片文件到服务器吗?
到目前为止,这是我使用 UIImagePickerController 所做的,在此阶段之前它工作正常:-
-(void)changeProfileImage:(UITapGestureRecognizer *)recognizer{
if (!_photoManager) {
_photoManager =[[SelectPhotoManager alloc]init];
}
[_photoManager startSelectPhotoWithImageName:@"Select Profile Image"];
__weak typeof(self)mySelf=self;
//Select Photo
_photoManager.successHandle=^(SelectPhotoManager *manager,UIImage *image){
mySelf.imgProfileImage.image = image;
NSData *data = UIImagePNGRepresentation(image);
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"imgProfile"];
};
}
请帮忙。谢谢。
// HTTP method to upload file to web server
-(void)initWithURL:(NSString *)url image:(NSData *)imageData andFileName:(NSString *)filename delegate:(id)del {
// your PHP server url
// your image data
NSString *urlString = url;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
// if access token defined for authrization
NSString *token = [NSString stringWithFormat:@"Bearer %@",[[[CommonUserDefault getUserInfo] objectForKey:@"token"] objectForKey:@"accessToken"]];
[request addValue:token forHTTPHeaderField: @"Authorization"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"files\"; filename=\"%@.jpg\"\r\n",filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
//return and test
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
id json = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];
NSLog(@"json upload picture = %@",json);
int statusCode = [[json objectForKey:@"statusCode"] intValue];
if(statusCode == <errorCode>){
}
}