简体   繁体   中英

upload image iphone

I am trying to upload uiimage from my phone application to server using php. 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

* -[CFString release]: message sent to deallocated instance 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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