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];
}
Related
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 want to upload images in server using ASIHTTPRequest library in my IPhone app.
For image upload, i am using ASIFormDataRequest to upload into server.
I have tried the below codes in my app but it couldn't working and image didn't uploaded in server.
NSURL *url = [NSURL URLWithString:#"http://www.anglerdemo.com/Appln/Aghaven_iphone/Uploadphoto.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
request.requestMethod = #"POST";
NSString *fileName = #"iphone.jpg";
[request addPostValue:fileName forKey:#"name"];
// Upload an image
UIImage *img = [UIImage imageNamed:fileName];
NSData *imageData = UIImageJPEGRepresentation(img, 90);
NSLog(#"imageData ==> %#", imageData);
[request setData:imageData withFileName:fileName andContentType:#"image/jpeg" forKey:#"image"];
[request setDelegate:self];
[request startAsynchronous];
I have tried above codes in my app, request successfully executed and i got the alert message in "requestFinished" delegate method of ASIHTTPRequest. but image didn't uploaded in server.
- (void)requestFinished:(ASIHTTPRequest *)request
{
[[[[UIAlertView alloc]
initWithTitle:#"Message"
message:#"Success!!!"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil]
autorelease]
show];
NSLog(#"success ==> ");
}
I have tried php file script for upload is below.
<?php
$uploaddir = 'upload_files/';
$file = basename($_FILES['image']['name']);
$uploadfile = $uploaddir . $file;
if(move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile))
{
echo "Photo has been saved successfully!…;
}
else {
echo "Failed";
}
?>
Please help in this regards.
Thanks!!!
I think the first thing I would do is add
NSLog(#"%#", [request responseString]);
to requestFinished: just to be sure you see "Failed" being logged. This will prove the problem is with the attempt to call move_upload_file, which I would guess is where things go wrong.
If that's the case, I would be pretty sure that the user your webserver runs as doesn't have write permissions to the upload_files directory.
I am developing an app that will request the profile picture URL of some users from Facebook servers, but I don't know how many users I will have (it might be 2 or it might be 20). Should I use ASIHTTPRequest with a loop and a synchronous request, or the API graph (with Facebook SDK for iOS) with a loop?
Trying using ASINetworkQueue. It will allow you to create a queue of ASIHTTPRequests that can still be started asynchronously. For example
- (void)getImages
{
if(!self.queue)
self.queue = [[[ASINetworkQueue alloc] init] autorelease];
NSArray* urlStringsToRequest = [NSArray arrayWithObjects:#"http://www.example.com/image1.png",#"http://www.example.com/image2.png",nil];
for(NSString* urlString in urlStringsToRequest)
{
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestDone:)];
[request setDidFailSelector:#selector(requestWentWrong:)];
[self.queue addOperation:request];
}
[self.queue go];
}
- (void)requestDone:(ASIHTTPRequest*)req
{
UIImage* image = [UIImage imageWithData:[req responseData]];
[imageArray addObject:image];
}
- (void)requestWentWrong:(ASIHTTPRequest*)req
{
NSLog(#"Request returned an error %#",[req error]);
}
Sorry for my english ^^
I would know how I can have a result from the google converter tool for currency, available on http://www.google.com/finance/converter?a=1&from=USD&to=GBP for example.
I have already tried something like that :
-(IBAction)getResponse:(id) sender{
NSString *myURLString = #"http://www.google.com/finance/converter?a=1&from=USD&to=GBP";
NSURL *myURL = [NSURL URLWithString: myURLString];
NSData *data = [NSData alloc];
data=[NSData dataWithContentsOfURL:myURL];
if(data!=nil && data != ""){
NSString *response = [[[NSString alloc] initWithData:data]]
[label setText: response];
}
else [label setText:#"not working"];
}
But when I click on my button, the label does not have a text (it is like it is empty).
Am I doing something wrong ?
Is it possible what I want to do ?
Thank you very much for your futures answers !
Olivier.
Yeah.. It is possible...
See the data that you are getting is an HTML file or can be treated as xml. Just parse that xml file and get the result. There ia lot of things in that file but you have to only extract
<div id=currency_converter_result>1 USD = <span class=bld>0.6118 GBP</span>
Hey try this URL
http://www.google.com/ig/calculator?hl=en&q=1USD=?INR
This URL will return the JSON
{lhs: "1 U.S. dollar",rhs: "44.5097254 Indian rupees",error: "",icc: true}
Just parse it. :)
Try...
NSData *data = [[NSData alloc] init];
Then check you've got something in response
NSLog(#"%#", response);
Better yet, remove the NSData alloc/init
Replace with
NSData *data=[NSData dataWithContentsOfURL:myURL];
And, you'll need to parse the return result, because you're not going to get back what you expect. Response will contain HTML page.
You better use an asynchronous request to avoid blocking your app. Easy using ASIHTTPRequest:
- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://www.google.com/finance/converter?a=1&from=USD&to=GBP"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
[label setText: responseString];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error]; //you can log the error description
[label setText:#"not working"];
}
NSString *myURLString = #"http://www.google.com/finance/converter?a=1&from=USD&to=GBP";
NSURL *myURL = [NSURL URLWithString: myURLString];
NSData *data = [NSData alloc];
data=[NSData dataWithContentsOfURL:myURL];
if(data!=nil){
NSString *response = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
NSLog(#"%#",response);
}
Try this.
This API is giving HTML responce.
I need to upload a single file. I have tried ASIRequest or whatever it is called, but would like to have something with a detailed guide.
You'll want to use ASIFormDataRequest. You'll send your file as an NSData object in the request. Here's an example of sending a JPEG. If you have some other file type, you'll need to convert it to an NSData object for transmission to the server.
NSString *filename = #"image.jpg";
UIImage *image = [UIImage imageNamed:#"image.jpg"];
NSData *fileData = UIImageJPEGRepresentation(image, 0.9);
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL urlWithString:#"http://www.yoursite.com"]];
[request setPostValue:filename forKey:#"name"];
[request setData:fileData forKey:#"file"];
[request setTimeOutSeconds:500];
[request setRequestMethod:#"POST"];
[request startSynchronous];
// handle response from server
The ASI docs go over this in the section titled "Sending a form POST with ASIFormDataRequest" - http://allseeing-i.com/ASIHTTPRequest/How-to-use