简体   繁体   English

通过php将图像从UIImagePicker上传到服务器

[英]Upload image from UIImagePicker to server via php

I have an UIImageView which load profile picture of the user. 我有一个UIImageView加载用户的个人资料图片。 I add some UIImagePicker features and now the user able to change the profile picture either by using camera or add from library. 我添加了一些UIImagePicker功能,现在用户可以通过使用相机或从库中添加来更改配置文件图片。 This is the code: 这是代码:

- (IBAction)cameraAction:(id)sender {
   UIImagePickerController *picker = [[UIImagePickerController alloc] init];
   picker.delegate = self;
   picker.allowsEditing = YES;
   picker.sourceType = UIImagePickerControllerSourceTypeCamera;

   [self presentViewController:picker animated:YES completion:NULL];
}

- (IBAction)libraryAction:(id)sender {
   UIImagePickerController *picker = [[UIImagePickerController alloc] init];
   picker.delegate = self;
   picker.allowsEditing = YES;
   picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

   [self presentViewController:picker animated:YES completion:NULL];
}

#pragma mark - Image Picker Controller delegate methods

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

   UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
   _profilePhoto.image = chosenImage;
   UIImageWriteToSavedPhotosAlbum(_profilePhoto.image, nil, nil, nil);

   [picker dismissViewControllerAnimated:YES completion:NULL];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

   [picker dismissViewControllerAnimated:YES completion:NULL];

}

Now then, I had php files to upload the current image to server. 现在,我有php文件将当前图像上传到服务器。 The problem is, the input parameter for the php is image file name. 问题是,php的输入参数是图像文件名。 I do looking around for quite some times and known that to get the image name, first I need to save it to apps document directory and rename it with the name I want later. 我确实四处寻找并知道要获取图像名称,首先我需要将其保存到应用程序文档目录并使用我想要的名称重命名它。 So can anyone tell me how to save this image to apps document directory, how to get it and how to rename it with the one I want. 所以任何人都可以告诉我如何将此图像保存到应用程序文档目录,如何获取它以及如何使用我想要的那个重命名它。 Thanks in advance 提前致谢

Don't think that saving the file to document directory will generate you new name. 不要以为将文件保存到文档目录会生成新名称。 When I save something to documents I generate unique file name. 当我将文件保存到文档时,我会生成唯一的文件名。

What I do is I pass to web server some generic name like "Upload.png". 我所做的是向Web服务器传递一些通用名称,如“Upload.png”。

Here is some pseudo code: 这是一些伪代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
   UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
   [picker dismissViewControllerAnimated:YES completion:NULL];


   ...
   NSData *data = UIImagePNGRepresentation(choosenImage);
   NSString *name = @"Uploaded.png"

   // Upload the nsdata to server
   [[PHPServer instance] uploadProfileImage:data named:name];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM