Retrieve the UIImage taken with takePicture - iphone

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

Related

How can i upload Photo from imagePickerController using ASIFormDataRequest?

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

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

Image rotated when I receive it on my server

I send an UIImage on my server, but when I reveive it, the image is rotated by 90°.
I maked this with the following code :
UIImageView *iv = [[UIImageView alloc] initWithImage:screenshot];
iv.center = CGPointMake(100.0, 100.0);
iv.transform = CGAffineTransformMakeRotation(3.14159265/2);
NSData *imageData = UIImagePNGRepresentation(iv.image);
// on créé la requete
NSURL *url = [NSURL URLWithString:#"http://myserver/test.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setData:imageData withFileName:#"tmp.png" andContentType:#"image/png" forKey:#"userfile"];
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request startAsynchronous];
As you can see, I try to rotate the UIImageView but there is no change when I display the UIImage on the server.
Thanks in advance.
EDIT : The image is taken with the method takePicture.
You are just rotating the display of UIImageView
To rotate the UIImage you have to use code like :
How to Rotate a UIImage 90 degrees?

Uploading image or a video to server as a raw data without encoding

Is it possible to upload an media(image & video ) directly to server without encoding which I am picking using UIImagepickerController, if so please suggest me how to do it.Please provide a sample for this.
In case of video upload you can send a file without encoding on the server
Here is the code for that:
if ([type isEqualToString:(NSString *)kUTTypeVideo] || [type isEqualToString:(NSString *)kUTTypeMovie])
{
NSURL *urlvideo = [info objectForKey:UIImagePickerControllerMediaURL];
}
//IN urlVideo You get the path of media file(video files)
NSString *str = [NSString stringWithFormat:#"%#/uploadVideo.php",appUrl];
NSURL *url =[NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:self.gameID forKey:#"gameId"];//value for gameID Parameter
[request setFile:strurl forKey:#"uploadfile"]; //setFile method is used for upload video files
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request startSynchronous];
NSLog(#"responseStatusCode %i",[request responseStatusCode]);
NSLog(#"responseStatusCode %#",[request responseString]);
I hope this might help you but please remember this does not work in case of image upload

How to upload image on Twitter

I want to upload image on Twitter.
please any one help me how we upload image on Twitter.
Please explain or provide code.
The following is to utilize Twitpic.
As said by others you have to start by looking at the API to understand the requests.
You can use Oliver Drobnik's Tutorial : Uploading UIImages to TwitPic which does it from scratch using NSMutableURLRequest
or you can use the asi-http-request which is a CFNetwork wrapper for HTTP requests
NSData *imageData = UIImagePNGRepresentation(imageToPost);
NSURL *twitpicURL = [NSURL URLWithString:#"http://twitpic.com/api/uploadAndPost"];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:twitpicURL] autorelease];
[request setData:imageData forKey:#"media"];
[request setPostValue:#"myUsername" forKey:#"username"];
[request setPostValue:#"myPassword" forKey:#"password"];
[request setPostValue:#"myMessage" forKey:#"message"];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestDone:)];
[request setDidFailSelector:#selector(requestFailed:)];
[request start];
You should look at the first.. first that way you understand what is happening.
You must use a "twitter-picture-provider" like TwitPic or TweetPhoto. They usually provide their own APIs as far as I know.
Twitter does not host image uploads at the moment. You have to use a third-party service. Yfrog and Twitpic are the two most popular on Twitter.
first import twitter frame work after that u write this code
TWTweetComposeViewControllerCompletionHandler completionHandler =^(TWTweetComposeViewControllerResult result)
{
switch (result) {
case TWTweetComposeViewControllerResultCancelled:
NSLog(#"twitter result:cancelled");
break;
case TWTweetComposeViewControllerResultDone:
NSLog(#"twitter result:sent");
}
[self dismissModalViewControllerAnimated:YES];
};
TWTweetComposeViewController *tvc = [[TWTweetComposeViewController alloc] init];
if(tvc)
{
[self addTweetContentContent:tvc];
tvc.completionHandler = completionHandler;
[self presentModalViewController:tvc animated:YES];
}
the local method is....
-(void)addTweetContentContent:(id)tvc
{
UIImage *image1 = UIGraphicsGetImageFromCurrentImageContext();
NSData *imageData = UIImagePNGRepresentation(image1);
UIImage *picture = [UIImage imageWithData:imageData];
or
UIImage *picture=[UIImage imageNamed:#"a.png"];
[tvc addImage:picture];
NSString *tweetText = #"write your comands";
[tvc setInitialText:tweetText];
}