1

有人请... #$%^ 请看看这个。几天通过调试器,使用带有 jpeg 表示的 setData。使用 ios4 资产库设置文件,尝试新的 PHP 脚本,删除 asiHTTPrequest 文件并确保我有新的。仍然一无所获......一半的代码是从这里或网络上其他地方的示例中组合起来的。

这里的目标是简单地从相机胶卷中挑选一张照片,然后上传,看起来很容易,我有一个不同的 PHP 脚本,它在桌面上运行良好,并从这里找到了一个,因为它更简洁,它可以从桌面也是如此。

所以完成图像拾取的覆盖

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;

// dealing with a still image
if(CFStringCompare((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo){

    editedImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];

    originalImage = (UIImage*) [info objectForKey:UIImagePickerControllerOriginalImage];

    /*
    if(editedImage){
        imageToSave = editedImage;
    } else {
        imageToSave = originalImage;
    }
    */
    chosenImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];

    [[picker parentViewController] dismissModalViewControllerAnimated:YES];

    //_imageData = [[NSData alloc] initWithData:UIImageJPEGRepresentation(originalImage, 0.0)];
    //_imageData = [[NSData alloc] initWithData:UIImageJPEGRepresentation(chosenImage.image, 0.0)];

    UIImage *im = [info objectForKey:@"UIImagePickerControllerOriginalImage"] ;
    UIGraphicsBeginImageContext(CGSizeMake(320,480)); 
    [im drawInRect:CGRectMake(0, 0,320,480)];
    _resizedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext();
    _imageData = [[NSData alloc] initWithData:UIImageJPEGRepresentation(_resizedImage, 0.0)];

    }
    [picker release];
}

然后上传方法。

-(void)uploadPhoto
{
//NSLog(@"image path inside uploadPhoto --> %@", _imagePath);
NSLog(@"uploadPhoto");


//NSLog(@"%@", imageData);

//_imageData = _imageData;

NSString *unescapedURL = @"http://dev.xxxx.com/upload.php";

NSString * escapedURL =
(NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                    NULL,
                                                    (CFStringRef)unescapedURL,
                                                    NULL,
                                                    (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                    kCFStringEncodingUTF8 );


NSURL *url = [NSURL URLWithString:unescapedURL];
//NSURL *url = [NSURL URLWithString:unescapedURL];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request setRequestMethod:@"POST"];
//[request setStringEncoding:NSUTF8StringEncoding];
//[request addPostValue:@"submit" forKey:@"Submit"];
//[request setPostValue:@"Submit" forKey:@"Submit"];
[request setData:_imageData withFileName:@"image4.jpg" andContentType:@"image/jpeg" forKey:@"photo"];
//[request setFile:_imagePath forKey:@"photo"];
//[request setFile:_imagePath withFileName:@"image5.png" andContentType:@"image/png" forKey:@"photo"];
[request setDidFailSelector:@selector(requestFailed:)];
[request setDidFinishSelector:@selector(requestFinished:)];
[request setTimeOutSeconds:500];
[request startAsynchronous];

NSError *error = nil;
NSString *theString = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
if( theString )
{
    NSLog(@"Text=%@", theString);
}
else 
{
    NSLog(@"Error = %@", error);
    NSString *localized = [error localizedDescription];
    NSString *localizedFail = [error localizedFailureReason] ? [error localizedFailureReason] : NSLocalizedString(@"not it", nil);
    NSLog(@"localized error--> %@", localized);
    NSLog(@"localizedFail--> %@", localizedFail);

}

[escapedURL release];

}

然后是完成/失败选择器

-(void)requestFinished:(ASIFormDataRequest *)request
{
    NSLog(@"requestFinished");
    NSString *respondingString = [request responseString];
    NSLog(@"response string--> %@", respondingString);

    NSData *responseData = [request responseData];
    NSLog(@"%@", responseData);
}

-(void)requestFailed:(ASIFormDataRequest *)request
{
    NSLog(@"requestFailed");
    //NSError *error = [request error];
    //NSLog(@"%@", [error description]);
}

帮助!溺水...

4

1 回答 1

0

这是PHP的问题。

move_uploaded_file($_FILES["file"]["tmp_name"]

是问题所在。

如果你看这个

[request setData:_imageData withFileName:@"image4.jpg" andContentType:@"image/jpeg" forKey:@"photo"];

它正在改变 POST 所以标准......

move_uploaded_file($_FILES["file"]["tmp_name"]

需要是

move_uploaded_file($_FILES["photo"]["tmp_name"]

添加

error_reporting(E_ALL);
ini_set("display_errors", 1); 
print_r($_FILES);

到PHP让我看到..

response string--> Array
(
    [photo] => Array
        (
            [name] => image4.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/phpCSXJgl
            [error] => 0
            [size] => 150854
        )

 )

在由...定义的选择器中

[request setDidFinishSelector:@selector(requestFinished:)];

我将从这里做的是将 PHP 恢复到以前的位置

move_uploaded_file($_FILES["file"]["tmp_name"]

并将 setFile 调用更改为

[request setData:_imageData withFileName:@"image4.jpg" andContentType:@"image/jpeg" forKey:@"file"];

这个世界一切都会好起来的,我要去买点吃的。干杯!

于 2011-09-26T22:25:55.780 回答