Pic无法上传相机图片,但上传截图和保存的图片

时间:2015-02-10 14:25:13

标签: php ios upload camera pic

我有一个脚本,目前上传从互联网或截图中保存的图片。但是,用iPhone相机拍摄的任何照片都无法上传。那为什么会这样?这是记忆问题吗?

<?php
$uploaddir = './upload/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (is_uploaded_file($_FILES['userfile']['tmp_name']))
{
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
{
    echo "yes";
}
}
else
{
echo $_FILES['userfile']['error']; //just returns "1"

}
?>

这是iOS:

 NSUserDefaults *defaujez = [NSUserDefaults standardUserDefaults];
    NSString *rambli = [defaujez objectForKey:@"getpicforfu"];      


    NSData *imageData =UIImageJPEGRepresentation(imageView.image, 90);
    NSString *imgPath= @"http://www.mywebsite.com/script.php";

        NSString *filenames = rambli;
        NSLog(@"%@", filenames);
        NSString *urlString = imgPath;

        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];

        NSString *boundary = @"---------------------------14737809831466499882746641449";
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

        NSMutableData *body = [NSMutableData data];

        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];

        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", rambli] dataUsingEncoding:NSUTF8StringEncoding]];

       // [body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        // setting the body of the post to the reqeust
        [request setHTTPBody:body];
        // now lets make the connection to the web
        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
        if([returnString isEqualToString:@"yes"])
        {
             [self finitop];

        }
        else {
            [spinner stopAnimating];
            rambolrqa.text = @"Posted Empty Because Upload Failed";

        }

1 个答案:

答案 0 :(得分:0)

这可以通过在iOS上将其压缩到合理的大小来解决它。

NSData *imageDataf = [[NSData alloc] initWithData:UIImageJPEGRepresentation((imageView.image), 0.5)];
int imageSize = imageDataf.length;

if (imageSize >50000) {
    //35414 normal
    //952683 camera
    if (imageSize >2000000) {
NSData *imgData= UIImageJPEGRepresentation(imageView.image,0.00001);
UIImage *image=[UIImage imageWithData:imgData];
imageView.image = image;
    }
    else {
        NSData *imgData= UIImageJPEGRepresentation(imageView.image,0.001);
        UIImage *image=[UIImage imageWithData:imgData];
        imageView.image = image;
    }
}