how to show video or movie file into UIImagePickerController? - iphone

I am using UIImagePickerController that gives user to be able select an existing photo or use the camera to take an image at that time. And i can show that image in my application with UIImageView.
Now i want to use this ability for movies also. But i couldn't find any way to show the selected movie as an image in my app, just like the Photos app. as you know you can see photos and movies in the same list.

-(IBAction) selectImageSelected : (id)sender
{
actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select Image"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Take Picture", #"Select from gallery", nil];
[actionSheet showInView:self.parentViewController.tabBarController.view];
}
- (void)actionSheet:(UIActionSheet *)_actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex==0)
{
if( ![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] )
{
[AlertHandler showAlertMessage:[ErrorMessages getCameraNotFoundMsg] withTitle:#"No Camera Detected"];
return;
}
else
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imagePicker animated:YES];
}
}
else if(buttonIndex==1)
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:imagePicker.sourceType];
[self presentModalViewController:imagePicker animated:YES];
}
else
{
[actionSheet dismissWithClickedButtonIndex:2 animated:YES];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"]){
// UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSLog(#"found an image");
// [UIImageJPEGRepresentation(image, 1.0f) writeToFile:[self findUniqueSavePath] atomically:YES];
// SETIMAGE(image);
CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:#"/Documents"]]);
}
else if ([mediaType isEqualToString:#"public.movie"]){
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(#"found a video");
NSData *webData = [NSData dataWithContentsOfURL:videoURL];
//NSData *video = [[NSString alloc] initWithContentsOfURL:videoURL];
// [webData writeToFile:[self findUniqueMoviePath] atomically:YES];
CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:#"/Documents"]]);
CGSize sixzevid=CGSizeMake(imagePicker.view.bounds.size.width,imagePicker.view.bounds.size.height-100);
UIGraphicsBeginImageContext(sixzevid);
[imagePicker.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
eventButton.imageView.image=viewImage;
// NSLog(videoURL);
}
[picker dismissModalViewControllerAnimated:YES];
}
/*
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo1
{
[imagePicker dismissModalViewControllerAnimated:YES];
imagePicker.view.hidden = YES;
eventButton.imageView.image = image;
imageSelected = YES;
}
*/
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[imagePicker dismissModalViewControllerAnimated:YES];
imageSelected = NO;
}

The -thumbnailImageAtTime:timeOption: method of MPMoviePlayerController looks like it might help you get an image to display.

To have the picker view with only movie files
replace
imagePicker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:imagePicker.sourceType];
with
picker.mediaTypes = [NSArray arrayWithObject:#"public.movie"];

Related

Save image from camera

This is how I show and save from my camera:
-(IBAction)Camera:(id)sender{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObjects: (NSString *) kUTTypeImage,nil];
imagePicker.allowsEditing = NO;
[self presentModalViewController:imagePicker animated:YES];
newMedia = YES;
}
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissModalViewControllerAnimated:YES];
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [self scaleThenRotateImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
imageView.image = image;
if (newMedia)
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:finishedSavingWithError:contextInfo:), nil);
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
// Code here to support video if enabled
}
}
But the problem is, I'm using a picker that uses ALAsset,that can multiselect then displays a checkmark on images selected.
So for example I take a picture from my camera(with the code above) then go to my camera roll to select images(multiple/single) with the imageTaken, it doesnt display any checkmark when I show the picker, so I'm assuming the solution is I need to save it by/with ALAsset.
Should I just change this:
UIImagePickerControllerOriginalImage
to
UIImagePickerControllerMediaMetadata
or what should I do/change? Thanks for the answers.

Error with UIImagePickerController

I have this code for send images on tweet. When I try the app, the picker works well but it doesn't grab the image. What's wrong?
- (IBAction)sendImageTweet:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
picker.allowsEditing = NO;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSData *imageData = UIImagePNGRepresentation(image);
[self dismissModalViewControllerAnimated:YES];
[tweetViewController addImage:(UIImage *)imageData];
[self presentModalViewController:tweetViewController animated:YES];
}
You should use the constant, not an NSString and the key.
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

Capture Video as well as Images in iPhone

I am using ImagePickerController for Video Capturing. I need to save the capturing video as image also.
So I selected AVCaptureSession for capturing Images. I'm not able to run AVCaptureSession as well as ImagePickerController. So I need to give the ImagePickerController as input to the AVCaptureSession.
But I don't know how to do that. Please Help me.. and suggest me for the right way.
I found only these two. Hope its helps.
http://www.iphonedevsdk.com/forum/iphone-sdk-development/24025-uiimagepickercontroller-videorecording-iphone-3gs.html
UIImagePickerController: Getting image capture from video
I was having same issue.. I did work on image and video and used below code for image capturing:
- (void) snapImage: (id) sendern
{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
ipc.delegate = self;
ipc.allowsImageEditing = YES;
[self presentModalViewController:ipc animated:YES];
}
And for video recording i used below code:
- (void) recordVideo: (id) sender
{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
ipc.delegate = self;
ipc.allowsEditing = YES;
ipc.videoQuality = UIImagePickerControllerQualityTypeMedium;
ipc.videoMaximumDuration = 30.0f; // 30 seconds
ipc.mediaTypes = [NSArray arrayWithObject:#"public.movie"];
// ipc.mediaTypes = [NSArray arrayWithObjects:#"public.movie", #"public.image", nil];
[self presentModalViewController:ipc animated:YES];
}
Hope will help you both code snippets.
- (IBAction) useCamera: (id)sender
{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;
[self presentModalViewController:imagePicker
animated:YES];
newMedia = YES;
}
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.popoverController dismissPopoverAnimated:true];
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:YES];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
imageView.image = image;
if (newMedia)
UIImageWriteToSavedPhotosAlbum(image, self,
#selector(image:finishedSavingWithError:contextInfo:),nil);
}
}
-(void)image:(UIImage *)image
finishedSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo
{
if (error)
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: #"Save failed"
message: #"Failed to save image"
delegate: self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
The Apple docs and sample code related to AVFoundation has several mistakes. See iOS 4: Take photos with live preview using AVFoundation for code that actually captures images from a live preview.

How to give a name to a saved Image from the imagePickerController

I'm using an imagePickerController to replace my existing images inside my view. The function works within the same IB-file. However I'd like to load the chosen image in another IB-File as well. I think that the solution is to give a name to the image when saved. After it is saved I'd like to call the image (by name) from my memory within my other IB-file.
Here's a snippit of code I'm using within the photopicker IB-file
-(IBAction)setPhoto{
image1.image = fotoView.image;
}
-(IBAction)getCameraPicture:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = YES;
picker.sourceType = (sender == takePictureButton) ? UIImagePickerControllerSourceTypeCamera :
UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController: picker animated:YES];
[picker release];
}
-(IBAction)selectExitingPicture
{
if([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker= [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
[picker dismissModalViewControllerAnimated:YES];
fotoView.image = image;
NSData* imdata = UIImagePNGRepresentation ( image );
UIImage* im8 = [UIImage imageWithData:imdata];
UIImageWriteToSavedPhotosAlbum(im8, nil, nil, nil);
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
}
within my other class I'd like to call this image by means of:
if (#some condition){
UIImage *img = [UIImage imageNamed:#"Name of the image.png"];
[image1 setImage:img];
}
Help is greatly appreciated
Method for image picking and saving it to the directory:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
[picker dismissModalViewControllerAnimated:YES];
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:#"%#/MyName.png",docDir];
NSData *data = [NSData dataWithData:UIImagePNGRepresentation(image)];
[data writeToFile:pngFilePath atomically:YES];
}
Lateron you can call it by using:
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:#"%#/MyName.png",docDir];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:pngFilePath];

How to open camera while I click the UIButton in iPhone?

I am trying to open the camera when I click the UIbutton in iPhone app. I want to store the captured image in a location that I specify.
You need to use something like this
- (IBAction)selectPhotos
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
[picker release];
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
imageView.image = image;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
- (IBAction)saveImage:(id)sender {
if(imageView.image) {
[self showProgressIndicator:#"Saving"];
UIImageWriteToSavedPhotosAlbum(imageView.image, self, #selector(finishUIImageWriteToSavedPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
}
}
use [picker dismissViewControllerAnimated:YES completion:nil];
instead of [[picker parentViewController] dismissModalViewControllerAnimated:YES];
- (IBAction) useCamera: (id)sender
{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;
[self presentModalViewController:imagePicker
animated:YES];
newMedia = YES;
}
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.popoverController dismissPopoverAnimated:true];
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:YES];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
imageView.image = image;
if (newMedia)
UIImageWriteToSavedPhotosAlbum(image, self,
#selector(image:finishedSavingWithError:contextInfo:),nil);
}
}
-(void)image:(UIImage *)image
finishedSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo
{
if (error)
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: #"Save failed"
message: #"Failed to save image"
delegate: self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
}