In my app I'm allowing the user to select if they want to choose a photo from the library, use the camera to take a photo, or use the camera to record a video.
After a photo has been selected or taken with the Camera, tapping Use will dismiss the UIImagePickerController and take the user to a new UIViewController.
I want to do the same for when a user records a video. Right now if the user taps the 'Use' button it just dismisses the picker.
How do I detect if the picker is set to record video?
Here's what I have tried:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
if (picker.mediaTypes == [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera] && picker.cameraCaptureMode == UIImagePickerControllerCameraCaptureModeVideo)
{
// Push to new view controller
// dismiss the picker
}
}
Is it possible to do this? What should I check for?
Thanks!
Refer to Apple's "Camera Programming Topics for iOS". The recommendation there is to implement imagePickerController:didFinishPickingMediaWithInfo: instead of imagePickerController:didFinishPickingImage:editingInfo: (which has been deprecated since iOS 3). Then, one can inspect the value of the info dictionary's UIImagePickerControllerMediaType key and compare against the appropriate types.
For example (note that you'll need to add the MobileCoreServices framework, and import its header):
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *pickedMediaType = info[UIImagePickerControllerMediaType];
NSString *movieType = (__bridge NSString *)kUTTypeMovie;
NSString *imageType = (__bridge NSString *)kUTTypeImage;
if ([pickedMediaType isEqualToString:movieType]) {
NSLog(#"movie was picked");
}
else if ([pickedMediaType isEqualToString:imageType]) {
NSLog(#"image was picked");
}
else {
NSLog(#"something else was picked");
}
[self dismissViewControllerAnimated:YES completion:nil];
}
In your application, you'd replace the logging statements with the appropriate actions (From your question, it sounds like you already have code to push to the new view controllers, so replace the logging statements accordingly). Also, note that you generally shouldn't compare Objective-C objects with ==. As I'm comparing strings, I use isEqualToString:.
use the below stated code that detects the recording of video an at the end of dismissal it goes to detailviewcontroller
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
//not production code, do not use hard coded string in real app
if ( [mediaType isEqualToString:#"public.image" ])
{
NSLog(#"Picked a photo");
}
//not production code, do not use hard coded string in real app
else if ( [ mediaType isEqualToString:#"public.movie" ])
{
NSLog(#"Picked a movie at URL %#", [info objectForKey:UIImagePickerControllerMediaURL]);
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(#">>>>>>>>>>> Url %#", [url absoluteString]);
RecordDetailiPhone *objDetail = [[RecordDetailiPhone alloc] initWithNibName:#"RecordDetailiPhone" bundle:nil];
[objDetail saveVideoFileToDeractory:url];
[self.navigationController pushViewController:objDetail animated:YES];
}
}
Related
I'm relatively new to X Code
and
I m working on photo collage App , when i pick image from one picker then image picker is working properly but i want to pick different images from different imagepicker then image picker is not working properly
Anyone help me solve my problem. Here is my code
`
-(IBAction)imagepickMethod1:(id)sender
{
imagepicker=[[UIImagePickerController alloc]init];
imagepicker.delegate=self;
imagepicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagepicker animated:YES];
button1.tag=100;
}
-(IBAction)imagepickMethod2:(id)sender
{
imagepicker1=[[UIImagePickerController alloc]init];
imagepicker1.delegate=self;
imagepicker1.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagepicker1 animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
[picker dismissModalViewControllerAnimated:YES];
imagepicker.view.hidden=YES;
photoPreviewImageView.image=image;
}
-(void)imagePickerController1:(UIImagePickerController *)picker1 didFinishPickingImage:(UIImage *)image1 editingInfo:(NSDictionary *)editingInfo1
{
[picker1 dismissModalViewControllerAnimated:YES];
imagepicker.view.hidden=YES;
photoPreviewImageView1.image=image1;
}
`
//Take two imageView in your .h file
UIImageView *imgViewForFirstPicker;
UIImageView *imgViewForSecondPicker;
// Alloc these images in view did load
imgViewForFirstPicker = [[UIImaeView allo] initWithFrame:(give your rect)];
// Similarly for second imageView and add to both in self.view
-(IBAction)imagepickMethod1:(id)sender
{
UIImagePickerController *imagepicker=[[UIImagePickerController alloc]init];
imagepicker.delegate=self;
imagepicker.tag=100;
imagepicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagepicker animated:YES];
}
-(IBAction)imagepickMethod2:(id)sender
{
UIImagePickerController *imagepicker1=[[UIImagePickerController alloc]init];
imagepicker1.delegate=self;
imagepicker1.tag=101;
imagepicker1.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagepicker1 animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage: (UIImage*)image editingInfo:(NSDictionary *)editingInfo
{
[picker dismissModalViewControllerAnimated:YES];
if(picker.tag == 100)
imgViewForFirstPicker.image=image;
else
imgViewForSecondPicker.image=image;
}
try this one hope it will help you
UIImagePickerController creates a pretty heavy object, So I don't think it is advisable to create multiple instances of it. May be this is what is causing the problem if you are doing the same.. It would be nice if you could share your code as well so we can have more insights into your problem
It is advisable to just use on image picker. From the code you provided you are trying to create two different imagepicker delegate methods, but in reality only one of them will be called both times.
You should create on instance of imagePicker and change its tag depending on which image you needed changed and then in the -didFinishPickingImage check if (picker.tag == SOME_TAG) then set appropriately.
I am using UIImagePickerController in an iOS app to save an image in context using UIGraphicsBeginImageContext/UIGraphicsBeginImageContextWithOptions.
I recently noticed that I picture I had saved and then displayed at a later date was highly pixelated; when I went back with the same code and imported the photo again, I got a great image. After playing with this for a while on my device, I figured out that the quality of the image saved depends on WHEN I pressed the 'Choose' button on the 'Move and Scale' screen.
If the image is a larger image and still loading when I press the button, the image is pixelated... if I wait until the image loads, it is fine. My question is, is there any way I can control when the user presses the 'Choose' button - is there any way to force them to wait until after the image is fully loaded? Or is there another way that would be better to approach this issue?
- (void)choosePhoto {
//NSLog(#"%s", __FUNCTION__);
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//NSLog(#"%s", __FUNCTION__);
[self dismissModalViewControllerAnimated:YES];
UIImage *pickedImage = (UIImage*)[info objectForKey:#"UIImagePickerControllerEditedImage"];
[self setPersonImage:pickedImage];
}
- (void) setPersonImage:(UIImage *)pickedImage {
//NSLog(#"%s", __FUNCTION__);
NSManagedObjectContext *context = [[UIApplication sharedDelegate] managedObjectContext];
PersonImage *oldImage = person.image;
if (oldImage != nil) {
[context deleteObject:(NSManagedObject*)oldImage];
}
if (pickedImage != nil) {
// Create an image object for the new image.
PersonImage *newImageObject = [NSEntityDescription insertNewObjectForEntityForName:#"PersonImage" inManagedObjectContext:context];
[newImageObject setImage:pickedImage];
[person setImage:newImageObject];
}
else {
[person setImage:nil];
}
NSError *error;
if (![context save:&error]) {
exit(-1); // Fail
}
}
I suggest you implement your own Crop&Resize view controller.
set imagePicker.allowsEditing = NO.
create your view controller in - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info and pass selected image to your view controller.
push your view controller to image picker: [picker pushViewController:yourVC animated:YES]
The image captured by the iPhone 4 camera is ~5 MB in size and it takes a while to display/render it. One option is to compress the image using UIImageJPEGRepresentation().
If you do not want to compress the image, you can use UIWebView to display the images. The UIWebViewDelegate has a method - (void)webViewDidFinishLoad:(UIWebView *)webView, that hits after the rendering has been completed. You can enable the choose button in this method (which is disabled initially).
i am using UIImagepickerController to take pictures from camera..i am using my own view over the camera view by using the cameraoverlayview property...i want to take a picture from camera when user clicks on a button in my custom view...i am using the following code
- (void)viewDidLoad {
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imagePickerController animated:YES];
imagePickerController.showsCameraControls=NO;
imagePickerController.cameraOverlayView = self.customView;
}
- (IBAction) takePicture
{
[imagePickerController takePicture];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
}
but when i run this code it gives me error
ERROR: FigCreateCGImageFromJPEG returned -12905. Input (null) was 773625 bytes.
and i also get warnings for memory use level 2 and then level 1
and no image is displayed in the imageView.. can anyone help me what i might be doing wrong??
btw i am using iPhone 3G with iOS4.1 installed on it
UIImagePickerControllerOriginalImage is a symbol (an NSString * constant) and you can't assume its value. Use as is, without quotes. Other than that, if you properly retain and create both the picker and the image view, it should work.
So i got a iphone app that is a button and when you pressed it i want the camera view to pop up and i want to give the user a choice to take a picture or a video like in the default camera app the one switch thing.
Thanks!
What you are looking for is something called 'UIImagePickerController'
-(void) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if((UIButton *) sender == choosePhotoBtn) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentModalViewController:picker animated:YES];
}
This method is the what would be called when you press your button(s), what i am doing here is I have two buttons, choosePhotoBtn, and takePhotoBtn, the both link to the same method.
If the button that was pressed (the sender) is choosePhotoBtn, then UIImagePickerControllerSourceTypeSavedPhotosAlbum is set as the UIImagePickerController's source type.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
bannerImage.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}
These two methods are delegate methods. They get called when the modelViewController holding the UIImagePickerController is dismissed.
I would recommend this site, as it has a brilliant tutorial on how to use these functions.
http://icodeblog.com/2009/07/28/getting-images-from-the-iphone-photo-library-or-camera-using-uiimagepickercontroller/
This is where i learnt how to use these classes, it seems like the site has had a wordpress comment attack on it at the moment, and firefox is blocking it for me, but if u can get to the page. Its a great resource.
I followed a great tutorial (http://iphone.zcentric.com/2008/08/28/using-a-uiimagepickercontroller/) on using a UIImagePickerController to get images from the Photo Album or Camera on the iPhone. The problem is, the tutorial is a bit dated and documentation cites a method used by the delegate as being depreciated since 3.0. The problem is, the documentation failed to provide clues as to what to use in place? The deprecated method is:
– imagePickerController:didFinishPickingImage:editingInfo:
The above method is used as follows:
- (void) imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)img editingInfo:(NSDictionary*)editInfo
{
image.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
QUESTION: What is currently used in place of the deprecated method?
Here's how to use the new image picker API in a nutshell.
First, you need a class declared like this since it's setting itself as the image picker delegate:
#interface MyClass : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
UIImagePickerController* imagePicker;
}
#property(nonatomic,retain) UIImagePickerController* imagePicker;
- (IBAction) takePicture:(id)sender;
#end
The method that brings up the image picker would go something like this. It's declared here as an IBAction so you can directly wire it to a control (like a button) in Interface Builder. It also checks so that if you're on an iPhone it brings up the camera interface but on an iPod Touch it brings up the gallery picker:
#import <MobileCoreServices/UTCoreTypes.h>
...
#synthesize imagePicker = _imagePicker;
...
- (void) takePicture:(id)sender
{
if (!_imagePicker) {
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
}
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
NSArray* mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
self.imagePicker.mediaTypes = mediaTypes;
} else {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.imagePicker.allowsImageEditing = YES;
}
[self presentModalViewController:self.imagePicker animated:YES];
}
Then you need these two methods:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
// MediaType can be kUTTypeImage or kUTTypeMovie. If it's a movie then you
// can get the URL to the actual file itself. This example only looks for images.
//
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
// NSString* videoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
// Try getting the edited image first. If it doesn't exist then you get the
// original image.
//
if (CFStringCompare((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {
UIImage* picture = [info objectForKey:UIImagePickerControllerEditedImage];
if (!picture)
picture = [info objectForKey:UIImagePickerControllerOriginalImage];
// **You can now do something with the picture.
}
self.imagePicker = nil;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
self.imagePicker = nil;
}
Quoting the Apple documentation:
imagePickerController:didFinishPickingImage:editingInfo:
Tells the delegate that the user picked an image. This method is optional. (Deprecated in iPhone OS 3.0. Use imagePickerController:didFinishPickingMediaWithInfo: instead.)