【问题标题】:Upload image from UIImagePicker to server via php通过 php 将图像从 UIImagePicker 上传到服务器
【发布时间】:2014-11-25 23:01:38
【问题描述】:

我有一个 UIImageView 加载用户的个人资料图片。我添加了一些 UIImagePicker 功能,现在用户可以通过使用相机或从库中添加来更改个人资料图片。这是代码:

- (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];

}

现在,我有 php 文件将当前图像上传到服务器。问题是,php 的输入参数是图像文件名。我确实环顾了很多次,知道要获取图像名称,首先我需要将其保存到应用程序文档目录,然后用我想要的名称重命名它。所以谁能告诉我如何将这张图片保存到应用程序文档目录,如何获取它以及如何用我想要的那个重命名它。提前致谢

【问题讨论】:

标签: ios objective-c uiimage uiimagepickercontroller document


【解决方案1】:

不要认为将文件保存到文档目录会为您生成新名称。 当我将某些内容保存到文档时,我会生成唯一的文件名。

我所做的是向网络服务器传递一些通用名称,例如“Upload.png”。

这是一些伪代码:

- (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];
}

【讨论】:

  • 那你能告诉我如何将choosenImage转换为NSData吗?对不起菜鸟问题:p @Krzysztof
  • 抱歉没收到问题?
  • 抱歉,不小心按了回车键:P 请查看我编辑的评论
  • 不用担心,请查看更新的答案。不确定您的 php 方法实现,可能是多部分编码。如果是这样,请看一下这个要点,看起来不错:gist.github.com/mombrea/8467128 您需要相应地更改 Content-Disposition 和 Content-Type。
  • 好的,我想我理解你的代码了。那么我需要传递给 php 的是 NSData 和 NSString 而不是附加到图像本身的字符串?因为我用的php只有字符串post参数
猜你喜欢
  • 2012-12-22
  • 1970-01-01
  • 2011-02-02
  • 2015-03-29
  • 2011-10-15
  • 1970-01-01
  • 2013-11-02
相关资源
最近更新 更多