How can i upload Photo from imagePickerController using ASIFormDataRequest? - iphone

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];

Related

Capture a video of 10 seconds length and upload it to server using ASIHTTPRequest

I need to capture a video which will be maximum length of 10 seconds, and also need to upload it to server using ASIHttpRequest,
how do I do that?
You can set the videoMaximumDuration property of image picker for this.
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.mediaTypes = #[(NSString *)kUTTypeMovie];
imagePicker.videoQuality = UIImagePickerControllerQualityTypeHigh;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.videoMaximumDuration = 10;
You can get the duration of the video by using this
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:selectedVideoUrl];
CMTime duration = playerItem.duration;
float seconds = CMTimeGetSeconds(duration);
NSLog(#"duration: %.2f", seconds);
You can upload it to server using
//server url to upload
NSURL *url = [NSURL URLWithString: URL];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setUseKeychainPersistence:YES];
//give your file path here and key
[request addFile:file_path forKey:#""];
[request setDelegate:self];
[request setDidFinishSelector:#selector(uploadRequestFinished:)];
[request setDidFailSelector:#selector(uploadRequestFailed:)];
[request startAsynchronous];
//successful uploaded
- (void)uploadRequestFinished:(ASIHTTPRequest *)request{
}
//when failed
- (void)uploadRequestFailed:(ASIHTTPRequest *)request{
NSLog(#" Error - Statistics file upload failed: \"%#\"",[[request error] localizedDescription]);
}
try this
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)mediaDict {
NSString *type = [mediaDict objectForKey:UIImagePickerControllerMediaType];
if ([type isEqualToString:(NSString *)kUTTypeVideo] ||
[type isEqualToString:(NSString *)kUTTypeMovie]) { // movie != video
NSURL *url = [mediaDict objectForKey:UIImagePickerControllerMediaURL];
NSData *data = [NSData dataWithContentsOfURL:videoURL];
// UPLOAD THIS DATA because you must convert video file to NSData. and must take Post method.
}
return nil;
}

Get image from web to UIImageView don't works

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!

How to send photo from gallery programmatically?

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

ASIHTTP method for posting a video

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"];

Retrieve the UIImage taken with takePicture

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];