iPhone SDK record video then email it - iphone

I was wondering if someone could show me how to record video and add it as attachment to mail composer in IOS. The more specific the better. Thanks.

.h file
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController : UIViewController <MFMailComposeViewControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
UIImagePickerController *imagePicker;
NSURL *videoURL;
}
-(IBAction)submitVideo;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
//set image picker
imagePicker = [[UIImagePickerController alloc]init];
NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
imagePicker.mediaTypes = [mediaTypes filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(SELF contains %#)", #"movie"]];
imagePicker.delegate = self;
imagePicker.videoMaximumDuration = 60.0;
imagePicker.allowsEditing = YES;
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
-(IBAction)submitVideo
{
[self presentViewController:imagePicker animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(#"movie captured %#", info);
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(sendMail) userInfo:nil repeats:NO];
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)sendMail
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[mail addAttachmentData:[NSData dataWithContentsOfURL:videoURL] mimeType:#"video/MOV" fileName:#"Video.MOV"];
[mail setSubject:#"video"];
[mail setMessageBody:#"body" isHTML:YES];
[mail setToRecipients:[NSArray arrayWithObject:#"myemail#jhd.com"]];
[self presentViewController:mail animated:YES completion:nil];
}else {
NSLog(#"Device is unable to send the request in its current state.");
}
}

Related

Play video from camera roll in SpriteKit SKVideoNode

I've got most of this working but I'm stuck with something that might be simple. Im trying to play a video from the camera roll in SpriteKit. A video will play from the main bundle, and the video picker also works until I choose the video. I think I'm locking the main thread because when choosing the video the picker freezes. You're help is extremely appreciated.
.h
#import <SpriteKit/SpriteKit.h>
#import "GameViewController.h"
#import <MobileCoreServices/UTCoreTypes.h>
#interface GameScene : SKScene<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
#end
.m
#import "GameScene.h"
#import <AVFoundation/AVFoundation.h>
#implementation GameScene {
AVPlayer *player;
SKVideoNode *videoNode1;
NSURL *fileURL;
NSString *moviePath;
UIViewController *viewController;
}
-(void)didMoveToView:(SKView *)view {
player = [AVPlayer playerWithURL: fileURL];
videoNode1 = [[SKVideoNode alloc] initWithAVPlayer:player];
videoNode1.size = CGSizeMake(self.frame.size.width,self.frame.size.height);
videoNode1.position = CGPointMake(self.frame.size.width/2.0f,self.frame.size.height/2.0f);
videoNode1.zPosition = 100.0f;
[self addChild:videoNode1];
player.volume = 1.0f;
[videoNode1 play];
}
- (void)getVideo {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
viewController = self.view.window.rootViewController;
[viewController presentViewController: imagePicker animated: YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
fileURL = (NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];
moviePath = [fileURL path];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil);
}
player = [AVPlayer playerWithURL: fileURL];
}
viewController = self.view.window.rootViewController;
[viewController dismissViewControllerAnimated:YES completion:nil];
NSLog(#"place video");
}
Answering my own question for others who want this feature. You must set up your node and player in the imagePicker method.
- (void)getVideo {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
viewController = self.view.window.rootViewController;
[viewController presentViewController: imagePicker animated: YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
fileURL = [info objectForKey:UIImagePickerControllerMediaURL];
[picker dismissViewControllerAnimated:YES completion:NULL];
player = [AVPlayer playerWithURL: fileURL];
videoNode1 = [[SKVideoNode alloc] initWithAVPlayer:player];
videoNode1.size = CGSizeMake(self.frame.size.width, self.frame.size.height);
videoNode1.position = CGPointMake(self.frame.size.width/2.0f, self.frame.size.height/2);
videoNode1.zPosition = 100.0f;
[self addChild:videoNode1];
player.volume = 1.0f;
[videoNode1 play];
}

how to capture video

How can i record video
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.allowsEditing = YES;
imagePickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];
imagePickerController.delegate = self;
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];
i tried this one but i am getting image capture and i am not getting video capture.
#import <MobileCoreServices/UTCoreTypes.h>
-(void)actionStartVideoRecord:(id)sender {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
return;
}
UIImagePickerController* imagePicker = [[[UIImagePickerController alloc] init] autorelease];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.videoQuality = UIImagePickerControllerQualityTypeHigh;
imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];
imagePicker.delegate = self;
[self presentModalViewController:imagePicker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mType = [info valueForKey:UIImagePickerControllerMediaType];
if([mType isEqualToString:#"public.movie"]){
[NSData dataWithContentsOfURL:[info objectForKey:#"UIImagePickerControllerMediaURL"]]
}
}
Once the imagePickerController is presented, start the videoCapture of the device.
For more info, see doc for UIImagePickerController
You can make a subclass of UIImagePickerController. There you can have a UIButton. On tapping that, you could start capturing the video.

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

Can't display UIImage from UIImagePickerController when using IBAction

I 'm new to xcode. I try to select an image from the UIImagePickerController and
then display this image pressing a button. But the simulator crashes.
In the .h I use
UIImage *dispimage; //in order to make a global image
-(IBAction) open;
-(IBAction) print;
In the .m I have
- (void)viewDidLoad {
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsImageEditing = YES;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
dispimage = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissModalViewControllerAnimated:YES];
}
-(IBAction) open{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
};
-(IBAction) print{
imageView.image=dispimage;
};
When the button "print" is pressed, the sdk crashes. :(
Any help would be appreciated
Thanks in advance
Here is the code to get images from UIImagePickerController
-(void)viewDidLoad
{
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsImageEditing = YES;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
dispimage = [[info objectForKey:UIImagePickerControllerOriginalImage]retain];
[picker dismissModalViewControllerAnimated:YES];
}
-(IBAction)open
{
if(self.imgPicker == nil)
{
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsImageEditing = YES;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
[self presentModalViewController:self.imagePicker animated:YES];
[self.imagePicker release];
}
-(IBAction) print
{
if(dispimage != nil)
{
imageView.image=dispimage;
}
};