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
Related
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;
}
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];
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"];
I am making one application in which one webservices need to be called, is it possible to call webservices related to JSON in background,when user press homebutton in iPhone
You can call asynchronous request using
NSMutableURLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"Your webservice URL here"]];
[[NSURLConnection alloc]initWithRequest:request delegate:self];
where you can implement delegate methods
In your Button Method
//yourURL is the webservice URL
[self performSelectorInBackground:#selector(loadDataFromWebservice:) withObject:yourURL];
-(void) loadDataFromWebservice : (NSString *)strUrl
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url=[[NSURL alloc] initWithString:strUrl];
NSData *data = [NSData dataWithContentsOfURL:imgUrl];
//Do your manipulations with data
//If you want to update any UI with the webservice data
[self performSelectorOnMainThread:#selector(assignDataViews:) withObject:responseObject waitUntilDone:YES];
[pool release];
}
//Return any object which you are comfortable with. I returned NSArray
-(void)assignDataViews : (NSArray *) yourObject
{
//Do all your UI changes here
}
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];
}