Set maximum recording duration,while video recording from iphone app - iphone

In my iPhone app,a user can record the video.I want to set the maximum allowed time of recording is to 30seconds.How to do that,any ideas ?
using this code
-(BOOL)startCameraControllerFromViewController:(UIViewController*)controller
usingDelegate:(id )delegate {
if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO)
|| (delegate == nil)
|| (controller == nil)) {
return NO;
}
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
cameraUI.allowsEditing = NO;
cameraUI.delegate = delegate;
[controller presentModalViewController: cameraUI animated: YES];
return YES;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:NO];
if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
NSLog(#"movie path %#",moviePath);
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self,
#selector(video:didFinishSavingWithError:contextInfo:), nil);
}
}
}
-(void)video:(NSString*)videoPath didFinishSavingWithError:(NSError*)error contextInfo: (void*)contextInfo {
if (error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Video Saving Failed"
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Video Saved" message:#"Saved To Photo Album"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}

You need to set the videoMaxiumDuration property on UIImagePickerController after configuring it for video recording.
The value is an NSTimeInterval which is specified in seconds, so you'll want to set it to 300 seconds if you want 5 mins of video.

Related

Overlay on iPhone camera

I want to make an overlay on camera of my iphone app.I have followed this tutorial
But it teaches about capturing the image,I need to implement it on recording the video.
In the tutorial it is simply
- (void)takePicture
{
[picker takePicture];
}
But I am not able to implement it on recording the video,Please help me with an example if you can
Now without overlay I am using this coding for recording the video[I wish to implement it with the above tutorial]
-(void)ViewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(onUploadVideoProgress:) name:#"UPLOADPROGRESS" object:nil];
self.mediaTypes=[NSArray arrayWithObjects:(NSString *) kUTTypeMovie,nil];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
videoRecorder = [[UIImagePickerController alloc] init];
videoRecorder.sourceType = UIImagePickerControllerSourceTypeCamera;
videoRecorder.delegate = self;
NSArray *mediaTypes1 = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
NSArray *videoMediaTypesOnly = [mediaTypes1 filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(SELF contains %#)", #"movie"]];
if ([videoMediaTypesOnly count] == 0)
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Sorry but your device does not support video recording"
delegate:nil
cancelButtonTitle:#"OK"
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet showInView:[[self view] window]];
}
else
{
if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear])
videoRecorder.cameraDevice = UIImagePickerControllerCameraDeviceRear;
videoRecorder.mediaTypes = videoMediaTypesOnly;
videoRecorder.videoQuality = UIImagePickerControllerQualityType640x480;
videoRecorder.videoMaximumDuration = 30.0f;
appDelegate.videoOrientation = #"portrait";
[self presentModalViewController:videoRecorder animated:YES];
}
}
else
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Sorry but your device does not support video recording"
delegate:nil
cancelButtonTitle:#"OK"
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet showInView:[[self view] window]];
}
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.videoURL=[[info objectForKey:UIImagePickerControllerMediaURL] path];
self.videoData=[NSData dataWithContentsOfURL:[info objectForKey:UIImagePickerControllerMediaURL]];
[self dismissModalViewControllerAnimated:YES];
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType compare:(NSString*)kUTTypeMovie] == NSOrderedSame) {
NSURL *mediaUrl = [info objectForKey:UIImagePickerControllerMediaURL];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:mediaUrl];
moviePlayer.shouldAutoplay=NO;
UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:0.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
appDelegate.selectedVideo=[NSDictionary dictionaryWithObjectsAndKeys:
self.videoURL,#"videourl",
self.videoData,#"videodata",
thumbnail,#"thumbdata",
nil ];
}
AddOrEditVideoDetails *controller = [[AddOrEditVideoDetails alloc]initWithNibName:#"AddOrEditVideoView"bundle:nil];
[[self navigationController] pushViewController:controller animated:YES];
}
UIImagePickerController already has API for recording video. You can call
– startVideoCapture
– stopVideoCapture
to record video. Check the API

How to save a video using code in objective c?

I have captured a video using objective c,however I am not able to save it in the iphone photos library.I dont want to use the AlAssets library,I want to use the image picker only.I have seen a lot of methods on stack overflow and other sites but they either use storage location path(which is not mentioned what it is) or they dont work.
This is my piece of code.
-(IBAction)Onclick:(id)sender
{
UIImagePickerController *imagePicker = [[UIImagePickerController
alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)
kUTTypeMovie];
imagePicker.delegate = self;
//UISaveVideoAtPath
imagePicker.allowsImageEditing = NO;
[self.view addSubview:imagePicker.view];
[imagePicker viewWillAppear:YES];
CGRect overlayFrame = CGRectMake(0, 380, 320, 44);
//UILabel *lbl=[[UILabel alloc]init];
UIView *baseView = [[[UIView alloc] init] autorelease];
baseView.backgroundColor =[UIColor greenColor];
//lbl.text=#"dfgfdgfd";
baseView.frame=overlayFrame;
//view.delegate = self;
//view.picker = picker;
//[view customize];
imagePicker.cameraOverlayView = baseView;
[self presentModalViewController:imagePicker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"]){
UIImage *picture = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(picture, nil, nil, nil);
}
else if ([mediaType isEqualToString:#"public.movie"]){
NSURL *url = [[[info objectForKey:UIImagePickerControllerMediaURL] copy] autorelease];
// ALAssetsLibrary* library = [[[ALAssetsLibrary alloc] init] autorelease];
// [library writeVideoAtPathToSavedPhotosAlbum:url
// completionBlock:^(NSURL *assetURL, NSError *error){/*notify of completion*/}];
UISaveVideoAtPathToSavedPhotosAlbum(url, nil, nil, nil);
}
[self dismissModalViewControllerAnimated:YES];
}
Try this:
- (void) imagePickerController: (UIImagePickerController *) picker
didFinishPickingMediaWithInfo: (NSDictionary *) info
{
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
// Handle a movie capture
if (CFStringCompare (( CFStringRef) mediaType, kUTTypeMovie, 0)
== kCFCompareEqualTo)
{
NSString *moviePath = [[info objectForKey:
UIImagePickerControllerMediaURL] path];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath))
{
UISaveVideoAtPathToSavedPhotosAlbum (moviePath,self, #selector(video:didFinishSavingWithError:contextInfo:), nil);
}
}
}
}
- (void)video:(NSString*)videoPath didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo
{
if (error)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Photo/Video Saving Failed" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles: nil, nil];
[alert show];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Photo/Video Saved" message:#"Saved To Photo Album" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
}
}
Hope it helps you.
Try this:
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:videoURL completionBlock:^(NSURL *assetURL, NSError *error){
/*notify of completion*/
NSLog(#"AssetURL: %#",assetURL);
NSLog(#"Error: %#",error);
if (!error) {
//video saved
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:error.domain delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}];

UIPopoverController How to fix for iPad?

I am getting an error / crash on iPad only when using the following code, iPhone works fine.
It is basically when trying to load the gallery to pick an image.
The debug error is;
"On iPad, UIImagePickerController must be presented via UIPopoverController'"
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Error accessing photo library"
message:#"Device does not support a photo library"
delegate:nil
cancelButtonTitle:#"Abort!"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
if (buttonIndex == 1) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType =UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Error accessing Camera"
message:#"Device does not support a Camera"
delegate:nil
cancelButtonTitle:#"Abort!"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(#"the info opf picker is %#",info);
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
NSData *imageData;
if ([info objectForKey:UIImagePickerControllerEditedImage]) {
UIImage *image2=[info objectForKey:UIImagePickerControllerEditedImage];
//UIImage *myscaledImage=[self scaleImage:image2 toSize:CGSizeMake(18, 24)];
//imageData = UIImagePNGRepresentation(myscaledImage);
imageData=UIImageJPEGRepresentation(image2, .4);
NSLog(#"the lenght of the edited image data is %d",[imageData length]);
}
else {
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
//UIImage *myscaledImage=[self scaleImage:image toSize:CGSizeMake(18, 24)];
//imageData = UIImagePNGRepresentation(myscaledImage);
imageData=UIImageJPEGRepresentation(image, .6);
NSLog(#"the lenght of the image dataa is %d",[imageData length]);
}
NSDictionary *tempDict=[NSDictionary dictionaryWithObjectsAndKeys:imageData,#"Image",#"Theme",#"Cell_Text",nil];
// NSLog(#"the custom array%# and the row =%d", customArray,currentCameraRow);
[customArray replaceObjectAtIndex:currentCameraRow withObject:tempDict];
[pool drain];
//NSLog(#"")
[self startThread];
[picker dismissModalViewControllerAnimated:YES];
[myCustomTableView reloadData];
NSLog(#"size of myObject: %zd", malloc_size(myCustomTableView));
}
Could anyone be so kind as to help with the code above ? I am guessing an if/else statement but have not the first idea of what it would be and where to place it.
Thanks in advance,
Chris
EDIT - Ok I now have it working the load from gallery now loads 'Photos' in a small window, where I can select, however there is no 'Select' or 'Choose' button, if I click the photo, there is no tick, to say it is selected, however it does select it, but to dismiss I click outside the window... So it works, but I am guessing I have missed something out, here is the code;
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
if(popoverController != nil)
{
[popoverController release];
popoverController = nil;
}
UIPopoverController* popover = [[UIPopoverController alloc] initWithContentViewController:picker];
popoverController = popover;
popoverController.delegate = self;
[popover presentPopoverFromRect:CGRectMake(0, 0, 200, 800)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
else
{
[self presentModalViewController:picker animated:YES];
}
// [self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Error accessing photo library"
message:#"Device does not support a photo library"
delegate:nil
cancelButtonTitle:#"Abort!"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
if (buttonIndex == 1) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType =UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Error accessing Camera"
message:#"Device does not support a Camera"
delegate:nil
cancelButtonTitle:#"Abort!"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(#"the info opf picker is %#",info);
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
NSData *imageData;
if ([info objectForKey:UIImagePickerControllerEditedImage]) {
UIImage *image2=[info objectForKey:UIImagePickerControllerEditedImage];
//UIImage *myscaledImage=[self scaleImage:image2 toSize:CGSizeMake(18, 24)];
//imageData = UIImagePNGRepresentation(myscaledImage);
imageData=UIImageJPEGRepresentation(image2, .4);
NSLog(#"the lenght of the edited image data is %d",[imageData length]);
}
else {
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
//UIImage *myscaledImage=[self scaleImage:image toSize:CGSizeMake(18, 24)];
//imageData = UIImagePNGRepresentation(myscaledImage);
imageData=UIImageJPEGRepresentation(image, .6);
NSLog(#"the lenght of the image dataa is %d",[imageData length]);
}
NSDictionary *tempDict=[NSDictionary dictionaryWithObjectsAndKeys:imageData,#"Image",#"Theme",#"Cell_Text",nil];
// NSLog(#"the custom array%# and the row =%d", customArray,currentCameraRow);
[customArray replaceObjectAtIndex:currentCameraRow withObject:tempDict];
[pool drain];
//NSLog(#"")
[self startThread];
[picker dismissModalViewControllerAnimated:YES];
[myCustomTableView reloadData];
NSLog(#"size of myObject: %zd", malloc_size(myCustomTableView));
}
-(UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize {
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
}
The error is very clear:
On iPad, UIImagePickerController must be presented via UIPopoverController'
You need to put the image picker in a popover controller when run on the iPad. This is true when the sourceType is for the photo library. The camera based image picker can be shown as a full screen modal view controller.
This isn't an issue on the iPhone because the iPhone doesn't support the UIPopoverController.
The sample app PrintPhoto demonstrates how to show the image picker in a popover. Specifically, see PrintPhotoViewController.m.
Do something like this:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//present as a pop over vc
} else {
//present as a modal vc
}
To present a popover you might use this method
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated
You can read the documentation at this link link

how to prevent saving picture from camera roll again

May be this question is too easy or I did not know how easy it will be.
I have an actionsheet to allow the user to choose how if they want to take a photo or select from camera.
The app will save user taken photo regardless is selected from camera roll or taken from camera. Am I missing something?
Thanks for reading my question.
#pragma mark - photo select
-(IBAction)showCameraAction:(id)sender
{
if(![UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#""
delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil
otherButtonTitles:#"Choose Photo From Library", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
actionSheet.tag =1;
[actionSheet showInView:[self.view superview]];
}
else
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#""
delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil
otherButtonTitles:#"Take Photo With Camera", #"Choose Photo From Library", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
actionSheet.tag =1;
[actionSheet showInView:[self.view superview]];
}
}
- (void)getPhotoFromSource:(UIImagePickerControllerSourceType)sourceType;
{
//NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
if ([UIImagePickerController isSourceTypeAvailable:sourceType])
{
//NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//picker.mediaTypes = mediaTypes;
picker.delegate = self;
//picker.allowsEditing = YES;
picker.sourceType = sourceType;
[self presentModalViewController:picker animated:YES];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error accessing media" message:#"Device doesn't support media source" delegate:nil cancelButtonTitle:#"Drat" otherButtonTitles:nil, nil];
[alert show];
}
}
#pragma mark UIImagePickerController delegate methods
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.lastChosenMediaType = [info objectForKey:UIImagePickerControllerOriginalImage];
imageFrame=fImageView.frame;
//UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];
UIImage *orginalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImage *shrunkenImage = shrinkImage(orginalImage,imageFrame.size);
self.fImage = shrunkenImage;
fImageView.image = frogImage;
//answer fix, to prevent saving picture from cameraroll again.
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
{
UIImageWriteToSavedPhotosAlbum(orginalImage, self, #selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil);
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
//NSString *message;
//NSString *title;
// if (!error) {
// title = NSLocalizedString(#"SaveSuccessTitle", #"");
// message = NSLocalizedString(#"SaveSuccessMessage", #"");
// } else {
// title = NSLocalizedString(#"SaveFailedTitle", #"");
// message = [error description];
// }
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
// message:message
// delegate:nil
// cancelButtonTitle:NSLocalizedString(#"ButtonOK", #"")
// otherButtonTitles:nil];
// [alert show];
}
Try this code.. Its working properly from my side.
- (IBAction) uploadPhoto:(id)sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#""
delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil
otherButtonTitles:#"Use Photo from Library", #"Take Photo with Camera", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
actionSheetAction = ActionSheetToSelectTypeOfSource;
[actionSheet showInView:self.view];
[actionSheet release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
UIImagePickerControllerSourceType sourceType;
if (buttonIndex == 0) {
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
} else if(buttonIndex == 1) {
sourceType = UIImagePickerControllerSourceTypeCamera;
}else {
// Cancel
break;
}
if([UIImagePickerController isSourceTypeAvailable:sourceType]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = sourceType;
picker.delegate = self;
if (sourceType == UIImagePickerControllerSourceTypeCamera) {
picker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
}
picker.allowsImageEditing = NO;
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
#pragma mark -
#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
NSString *imgPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"image1.png"]];
//COnvert it to NSData before saving and then save it
NSData *imgData = UIImagePNGRepresentation(image);
[imgData writeToFile:imgPath atomically:YES];
[self dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissModalViewControllerAnimated:YES];
}
you need to implement UIImagePickerControllerDelegate
Check this function
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{}
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"]){
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
}
Now use this UIImage
To save this Image You have to specify a path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"photo.png"]];
//COnvert it to NSData before saving and then save it
NSData *webData = UIImagePNGRepresentation(editedImage);
[webData writeToFile:imagePath atomically:YES];

How to take picture from Camera & saved in Photo Gallery by programmatically?

In iPhone Apps, I want to take pictures from Camera & save into Photo Gallery by programmatically. Is it possible in iPhone Simulator?
so please tell me any link or materials you have.
Thanks in advance
You would use uiimagepickercontroller for this purpose. First of all, import the MobileCoreServices.framework. Then pull up the camera:
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSArray *media = [UIImagePickerController
availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
if ([media containsObject:(NSString*)kUTTypeImage] == YES) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
[picker setMediaTypes:[NSArray arrayWithObject:(NSString *)kUTTypeImage]];
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
//[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unsupported!"
message:#"Camera does not support photo capturing."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unavailable!"
message:#"This device does not have a camera."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
Then, save the photo by implementing the uiimagepickercontrollerdelegate and a save function:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"Media Info: %#", info);
NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
if([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
UIImage *photoTaken = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//Save Photo to library only if it wasnt already saved i.e. its just been taken
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(photoTaken, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
}
}
[picker dismissModalViewControllerAnimated:YES];
[picker release];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
UIAlertView *alert;
//NSLog(#"Image:%#", image);
if (error) {
alert = [[UIAlertView alloc] initWithTitle:#"Error!"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}
More info on image:didFinishSaving.... can be found here
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html
Also have a look at this post
https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more
it is not possible in simulator.You have to use the device.
I do hope you realize that the simulator does not have a camera..... So I guess thats not possible. You can definitely use a device.... You can make use of UIImagePickerController for capturing images.
There are 3 prerequesites needed to take a picture programmatically:
The device needs to have a camera check by
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
Camera controls need to be hidden (picker is a UIImagePickerController)
picker.showsCameraControls = NO;
use the takePicture from UIImagePickerController
[picker takePicture];
Side note because it did bug me for a few minutes, the takePicture method also dismisses the view.
Also posted a more complete answer here Other Stack answer