上传图片iphone

时间:2012-03-15 12:13:08

标签: iphone ios nsurlconnection

我正在尝试使用php将uiimage从我的手机应用程序上传到服务器。但有些如何不起作用。有人可以帮助我。我的代码:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
            request.delegate = self;
            request.tag = 33;
            [request setPostValue:[NSString stringWithFormat:@"%@.png",responseString] forKey:@"name"];
            NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
            NSString *docDirectory = [sysPaths objectAtIndex:0];
            NSString *filePath = [NSString stringWithFormat:@"%@/MyImage.png", docDirectory];
            [request setFile:filePath forKey:@"photo"];

            [request startAsynchronous];
            [url release];
            NSError *error = [request error];
            if (!error) {
                NSString *response = [request responseString];
                NSLog(@"aResponse:%@",response);

            }

if(move_uploaded_file($_FILES['photo']['tmp_name'],$target_path)) {
    echo "The file ". basename( $_FILES['photo']['tmp_name'])." has been uploaded";
} 
else {
    echo "There was an error uploading the file, please try again!";
}

>

在ASI请求文件中使用nszombie调试时出现以下错误

* - [CFString release]:发送到解除分配的实例0xd1dfd80的消息

3 个答案:

答案 0 :(得分:1)

请勿释放URL:请求将以异步方式运行,因此在您发布时不会使用它。

答案 1 :(得分:0)

您正在设置文件名但不是文件内容:

[request setData: UIImageJPEGRepresentation(imageView.image, 1.0) withFileName:filename andContentType:@"image/jpeg"

请参阅此Stackoverflow回答。

答案 2 :(得分:0)

你可以在这里查看my answer它是否有效也可能适合你?

NSData *imgData = UIImagePNGRepresentation(YourUIImageObject);

NSURL *url = @"yourURL";

ASIFormDataRequest *currentRequest = [ASIFormDataRequest requestWithURL:url];
[currentRequest setPostFormat:ASIMultipartFormDataPostFormat];
[currentRequest setRequestMethod:@"POST"];
[currentRequest addData:imgData withFileName:@"file" andContentType:@"image/png" forKey:@"yourFileNameOnServer"]; //This would be the file name which is accepting image object on server side e.g. php page accepting file
[currentRequest setDelegate:self];
[currentRequest setDidFinishSelector:@selector(uploadImageFinished:)];
[currentRequest setDidFailSelector:@selector(uploadImageFailed:)];  
[currentRequest startSynchronous];


-(void)uploadImageFinished:(ASIHTTPRequest*)request
{
     //Your request successfully executed. Handle accordingly.
}

-(void)uploadImageFailed:(ASIHTTPRequest*)request
{
     //Your request failed to execute. Handle accordingly.
}