I am using ASIHTTP method for uploading my video information.I got my video information in web data using the code
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"image picker did finish");
NSURL *videopath = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *webData = [NSData dataWithContentsOfURL:videopath];
[webData release];}
from this web data how can i post it using 'setdata'#"" for key#"";
anyone can help me?
You can use it like this :
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setData:webData withFileName:#"" andContentType:#"video/avi" forKey:#"video"];
Related
I know how to upload images to a server running PHP, but I am stuck on uploading video.
I have used this advice to upload my video file.
Posting method is all ok. What I get on the server is a file of 0 bytes.
My code is below:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfFile:[videoURL path]];
}
This videoData is passed in my POST method.
What should I do instead?
for posting video you need to use this function after image picker delegate
- (NSData *)generatePostDataForData:(NSData *)uploadData
{
// Generate the post header:
NSString *post = [NSString stringWithCString:"--AaB03x\r\nContent-Disposition: form-data; name=\"upload[file]\"; filename=\"somefile\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n\r\n" encoding:NSASCIIStringEncoding];
// Get the post header int ASCII format:
NSData *postHeaderData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
// Generate the mutable data variable:
NSMutableData *postData = [[NSMutableData alloc] initWithLength:[postHeaderData length] ];
[postData setData:postHeaderData];
// Add the image:
[postData appendData: uploadData];
// Add the closing boundry:
[postData appendData: [#"\r\n--AaB03x--" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
// Return the post data:
return postData;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//assign the mediatype to a string
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
//check the media type string so we can determine if its a video
if ([mediaType isEqualToString:#"public.movie"]){
NSLog(#"got a movie");
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *webData = [NSData dataWithContentsOfURL:videoURL];
[self post:webData];
[webData release];
}
for post video use this function
- (void)post:(NSData *)fileData
{
NSLog(#"POSTING");
// Generate the postdata:
NSData *postData = [self generatePostDataForData: fileData];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
// Setup the request:
NSMutableURLRequest *uploadRequest = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"http://www.example.com:3000/"] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 30 ] autorelease];
[uploadRequest setHTTPMethod:#"POST"];
[uploadRequest setValue:postLength forHTTPHeaderField:#"Content-Length"];
[uploadRequest setValue:#"multipart/form-data; boundary=AaB03x" forHTTPHeaderField:#"Content-Type"];
[uploadRequest setHTTPBody:postData];
// Execute the reqest:
NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:uploadRequest delegate:self];
if (conn)
{
// Connection succeeded (even if a 404 or other non-200 range was returned).
NSLog(#"sucess");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Got Server Response" message:#"Success" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
// Connection failed (cannot reach server).
NSLog(#"fail");
}
}
I have a big problem with very simple code.
I need to get a picture from a Facebook URL and put it on a UIImageView.
I don't know but don't works.
I have tried different links also (no Facebook links also don't works).
-(IBAction)setUserPhoto:(id)sender{
NSURL *url = [url initWithString:#"http://graph.facebook.com/100000769380612/picture?type=large"];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
if(image){
NSLog(#"Image OK");
} else if (!image) {
NSLog(#"Error");
}
[userPhoto setImage:image];
}
Thank you.
The string has a substitution variable in it, that hasn't been substituted:
http://graph.facebook.com/%#/picture?type=large
You are missing the alias (thats the error i get if i put this url into safari)
Use
NSString *urlString = [NSString stringWithFormat:#"http://graph.facebook.com/%#/picture?type=large", replacement]; //where replacement is the string that is supposed to go there
NSUrl *url = [NSUrl URLWithString:urlString];
Try the url in the web browser first, to ensure it exists.
You are missing something in url..it contain %# , please replace it with proper string
for eg :
https://graph.facebook.com/DoloresPark/picture?type=large
you URL should be some thing like the above one
-(IBAction)setUserPhoto:(id)sender{
NSString *user = #"DoloresPark";
NSString *urlString = [NSString
stringWithFormat:
#"http://graph.facebook.com/%#/picture?type=large",user];
NSURL *url = [NSURL URLWithString:urlString];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
if(image){
NSLog(#"Image OK");
} else if (!image) {
NSLog(#"Error");
}
[userPhoto setImage:image];
}
have you tried dataWithContentsOfURL:options:error: and reading the &error for any pointers?
The problem is that you are not making a connection to the URL.Use the following -
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:#"Your URL"]];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
your class should implement the NSURLConnectionDelegate -
Use the following delegate methods to get the data -
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
Hope it helps!
I need to perform "send photo" functionality. I have a web service method which takes image as parameter. And I need to perform the following:
User clicks on a button, and then photo gallery opens(also be cool to perform send photo from camera i.e camera opens user make photo and then send)
User select photo and click send(then my method should work).
Please tell me is there any way to do it?
- (IBAction)upload:(id)sender {
imagePickerViewController = [[UIImagePickerController alloc] init];
imagePickerViewController.delegate = self;
imagePickerViewController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePickerViewController animated:YES];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
requestType = 8;
[picker dismissModalViewControllerAnimated:YES];
// Access the uncropped image from info dictionary
int seconds = [[NSDate date] timeIntervalSince1970];
NSString *imageName = [NSString stringWithFormat:#"%d.png", seconds];
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSURL *url = [NSURL URLWithString:yourUploadLink];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSData* imageData=UIImageJPEGRepresentation(image, 1.0);
if (imageData) {
NSLog(#"imageData exists");
}
[request setData:imageData withFileName:imageName andContentType:#"image/png" forKey:#"userfile"];
[request setDelegate:self];
[request startAsynchronous];
// [picker release];
}
You need to do some research before you ask, but I'll direct you to look at the UIImagePickerController:
https://developer.apple.com/library/ios/#documentation/uikit/reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html
Yes, there is. You only have to build the app using: UIImagePicker for image selection (camera or library) and NSURLConnection for your communications tasks with your services.
You can start here: Apple iOS Dev Library
I use the delegate of UIImagePickerController for choosing a photo and put it to the server but the problem is i don't know how can i do that using ASIFormDataRequest and i don't know what key should use for the info parameter?
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *str = [NSString stringWithFormat:#"http://www.siteweb.com/api/v2/businesses/%#/pics.xml",entry.permalink];
NSURL *url = [NSURL URLWithString:str];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
request.delegate = self;
[request startAsynchronous];
[self dismissModalViewControllerAnimated:YES];
}
Like this:
[request addFile:(NSString *)filePath forKey:(NSString *)key];
Get the image like this beforehand:
UIImage *image = (UIImage *) [info
valueForKey:UIImagePickerControllerOriginalImage];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:filePath
atomically:YES];
Currently, I can retrieve the picture taken with takePicture in the function imagePickerController, but I want to use it outside of this function, and when I try to make a global variable to use this UIImage, I don't have any picture.
So how can I use this UIImage ?
My code :
- (void)imagePickerController:(UIImagePickerController *)aPicker didFinishPickingMediaWithInfo:(NSDictionary *)info {
screenshot = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
and I use it there :
NSData *imageData=[NSData dataWithData:UIImagePNGRepresentation(screenshot)];
NSURL *url = [NSURL URLWithString:#"http://myserver/test.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addData:imageData withFileName:#"tmp.jpg" andContentType:#"image/jpeg" forKey:#"userfile"];
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request startSynchronous];
NSLog(#"responseStatusCode %i",[request responseStatusCode]);
NSLog(#"responseStatusString %#",[request responseString]);
screenshot is my global variable and I receive a blank image on my server.
Thanks in advance.
I'm assuming you are implementing UIImagePickerControllerDelegate? If so, when imagePickerController:didFinishPickingMediaWithInfo: is called, you can get the image out of the dictionary passed in using:
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
If you want to retain the image, declare a property with retain for your viewcontroller class, and set it:
self.image = [info valueForKey:UIImagePickerControllerOriginalImage];