gpt4 book ai didi

php - 使用 ASIFormDataRequest 将 UIImage 从 UIImagePickerController 发送到 PHP 服务器

转载 作者:行者123 更新时间:2023-12-01 19:12:27 25 4
gpt4 key购买 nike

在当前的项目中,我实现了一个 UITableViewController收集某些数据并根据用户请求将其发送到后端服务器。
这一切都很好,花花公子……但是,我在上传图片时遇到了问题。

一旦我得到图像 om UIImagePickerController并发送它,服务器似乎有问题。如果我将其编码为 JPEG,PHP 似乎无法理解它...如果我将其编码为 PNG,整个请求似乎都被搞砸了。

但是,如果我选择项目中存在的任何其他图像(例如某些背景图像),上传过程将完美运行!这是奇怪的部分。一旦我使用 UIImagePickerController 中的图像,它就不起作用......即使我确实得到了图像(请参阅代码中的 addSubview,它有效)。

这是我的代码:

@interface ServiceViewController ()

@property (nonatomic) UIImage *image;
[...]

@end

@implementation ServiceViewController

@synthesize image = _image;
[...]

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.image = [[info objectForKey:UIImagePickerControllerOriginalImage] copy];

[self.tableView reloadData];

[self dismissModalViewControllerAnimated:YES];
}

- (void)sendToApi
{
// Send post data
NSString *urlString = [NSString stringWithFormat:@"%@/%@", API_BASE_DOMAIN2, @"requestservice"];
urlString = @"http://localhost/api/public_html/requestservice";

NSURL *url = [NSURL URLWithString:urlString];

ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
[request setUseKeychainPersistence:YES];
[request setPostValue:[self.problems objectAtIndex:self.problem] forKey:@"problem"];
[request setPostValue:[self stationName] forKey:@"station"];
[request setPostValue:self.otherInformation forKey:@"information"];
[request setPostValue:[NSNumber numberWithInt:self.lock] forKey:@"lock"];
[request setUploadProgressDelegate:self];
[request setDidFinishSelector:@selector(didCompleteRequestService:)];
[request setDidFailSelector:@selector(processFailed:)];
[request setDelegate:self];
[request setRequestMethod:@"POST"];

// Upload an image
[request addData:[NSData dataWithData:UIImageJPEGRepresentation(self.image, 0.9)] withFileName:@"img.jpg" andContentType:@"image/jpeg" forKey:@"picture"];


UIImageView *view = [[UIImageView alloc] initWithImage:self.image];
[view sizeToFit];
[self.view addSubview:view];

[request startAsynchronous];
}

- (void)setProgress:(float)newProgress {
// [prgressView setProgress:newProgress];
NSLog(@"New progress: %f", newProgress);
}

- (void)didCompleteRequestService:(ASIHTTPRequest *)request
{
NSLog(@"Request completed");
NSLog(@"Result: %@", request.responseString);
}

服务器端:
    var_dump($_FILES);

if ($this->_aPicture !== null) {
$sTarget = DIR_SERVICE . $this->_sFileNamePrefix . ".jpg";

var_dump($this->_aPicture);
var_dump($sTarget);

if (!move_uploaded_file($this->_aPicture['tmp_name'], $sTarget)) {
$this->returnHttpError(500);
}
}

$this->_writeDataToFile();

这是我的结果:
2013-03-21 00:04:04.859 AppName[15598:c07] New progress: 0.000000
2013-03-21 00:04:04.862 AppName[15598:c07] New progress: 1.000000
2013-03-21 00:04:04.863 AppName[15598:c07] Request completed
2013-03-21 00:04:04.863 AppName[15598:c07] Result: array(1) {
["picture"]=>
array(5) {
["name"]=>
string(7) "img.jpg"
["type"]=>
string(0) ""
["tmp_name"]=>
string(0) ""
["error"]=>
int(1)
["size"]=>
int(0)
}
}
array(5) {
["name"]=>
string(7) "img.jpg"
["type"]=>
string(0) ""
["tmp_name"]=>
string(0) ""
["error"]=>
int(1)
["size"]=>
int(0)
}
string(58) "/Users/paulp/Sites/api/service/K98XQPo7zx.jpg"
Sending error: 500

我尝试了很多不同的东西,例如:
  • How to upload image that was taken from UIImagePickerController
  • Upload picture selected using UIImagePickerController
    还有一些。

  • 我没有更改为例如MKNetworkKit,因为(还)没有时间或预算,所以这不是解决方案。

    我该如何解决这个问题?

    最佳答案

    保存所选图像

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {

    //Save selected image to document directory
    jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];
    [UIImageJPEGRepresentation(img, 1.0) writeToFile:jpgPath atomically:YES];
    [picker dismissModalViewControllerAnimated:YES];
    }

    然后使用 ASIFormDataRequestsetFile:forKey将图像发送到服务器的方法
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    if (jpgPath.length > 0) {
    [request setFile:jpgPath forKey:@"image"];
    }

    关于php - 使用 ASIFormDataRequest 将 UIImage 从 UIImagePickerController 发送到 PHP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15536652/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com