简体   繁体   English

上传图片iphone

[英]upload image iphone

I am trying to upload uiimage from my phone application to server using php. 我正在尝试使用php将uiimage从手机应用程序上传到服务器。 But some how it is not working. 但是有些不起作用。 Can someone please help me with that. 有人可以帮我吗。 My Code: 我的代码:

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!";
}

> >

Got Following error when debugged with nszombie in a file of ASI Request 在nszombie中调试ASI请求文件时出现以下错误

* -[CFString release]: message sent to deallocated instance 0xd1dfd80 * -[CFString版本]:消息发送到已释放实例0xd1dfd80

不要释放URL:请求将异步运行,因此在释放它时将不会完成该请求。

You're setting the filename but not the file contents: 您要设置文件名而不是文件内容:

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

See this Stackoverflow answer. 请参阅此Stackoverflow答案。

You can check my answer here and it worked so may be for you too ? 您可以在这里查看我的答案 ,它是否也可能适合您?

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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM