What to use in place of a deprecated UIImagePickerControllerDelegate method? - iphone

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.)

Related

How to share UIImagepickercontroller code for 2 buttons

In my project I am using the UIIamgepickercontroller to select an image from the library and load it into a UIImageView. I am doing this for 2 images so I have two buttons for each image view, but I do not want to replicate the code for image picker twice and I'm not sure how to implement so that the method knows which image view to load the image into. I think I need to use button tags? but can't find the right method.
here's my code:
.h
`#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface LoadViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
IBOutlet UIImageView *imageView;
IBOutlet UIImageView *imageView2;
}
- (IBAction)pick1;
- (IBAction)pick2;
- (void) getImage;
#end`
.m
#import "LoadViewController.h"
#implementation LoadViewController
UIImage *imageHandle;
- (IBAction)pick2 {
[self getImage];
imageView2.image = imageHandle;
}
- (IBAction)pick1{
[self getImage];
imageView.image = imageHandle;
}
- (void)getImage {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
imageHandle = image;
[picker.parentViewController dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker.parentViewController dismissModalViewControllerAnimated:YES];
}
#end
Another problem I'v been having is that the methods to catch whether I have selected an image or cancelled the view do not seem to work although if I comment out the entire method (void)imagePickerControllerDidCancel then it will cancel?!?
I'm in the early stages of learning this stuff and any help would be very much appreciated!
Thanks
You can identify the button with a tag (you can set this in Interface Builder), which is an arbitrary integer. The problem is that you've rejected the chance to receive a reference to the sender (the button); instead of - (IBAction)pick1, say - (IBAction)pick1:(id)sender. Now you can check the sender's tag (cast the sender to a UIView* so the compiler understands what you're doing).

How to access camera of iPhone using phonegap app

I have a simple application developed for android which takes a photo, accessing the camera of device.
I want the same application to be run on iPhone. For android we write permissions for accessing camera in manifest file. How do you access the camera in iPhone. Where do we need to set up it. Can anyone please help me?
Thanks in advance
Use the Media Capture API in the latest code, or wait for 0.9.6:
http://docs.phonegap.com/phonegap_media_capture_capture.md.html
For accessing the camera in iPhone, you can use following code.
// Camera
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.allowsImageEditing = YES;
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
//[self presentModalViewController:imgPicker animated:YES];
[objParent presentModalViewController:imgPicker animated:YES];
[imgPicker release];
Let me know in case of any difficulty.
Below are the delegate method for camera.
Delegate Name: UIImagePickerControllerDelegate, UINavigationControllerDelegate
Methods:
#pragma mark UIImagePickerController delegate
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *imgCapturePhoto = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[imgPhoto setImage:imgCapturePhoto];
[self dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
}
Cheers

using UIImagepickerController cameraoverlayview to take picture?

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.

Record Video/Take Picture switch in iPhone app?

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.

UIImagePickerController weirdness

UIImagePickerController is easy to use, but i'm all of a sudden finding it exasperating when I didn't find it so before. What's happening is that sometimes the imagePickerController:didFinishPickingImage:editingInfo delegate method does not seem to work -- the image will not show in the UIImageView even after the assignment was made. Sometimes it will, sometimes not, and furthermore, every single bit of example code i've tried (from the web, from the "Beginning iPhone 3 Development" book, etc.) exhibits the same problem. I'm at a loss as to why, and the problem happens on both my iPhone 3G as well as my 3GS, so I doubt that it's a hardware issue. These devices are running OS 3.1.2. The view controller is loaded from a xib file that contains one button and the UIImageView. I'd really like someone to tell me what stupid thing i'm obviously doing wrong :-)
Here is the code -- i've tried to make the smallest app I could that exhibits the problem:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface imagepickerViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
IBOutlet UIButton *button;
IBOutlet UIImageView *imageView;
}
#property (nonatomic, retain) UIImageView *imageView;
- (IBAction)takepic;
- (void)usePic:(UIImage *)pic;
#end
#import "imagepickerViewController.h"
#implementation imagepickerViewController
#synthesize imageView;
- (IBAction)takepic
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
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 *)info
{
[self usePic:image];
[picker dismissModalViewControllerAnimated:YES];
// after this method returns, the UIImageView should show the image -- yet very often it does not ...
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:YES];
}
- (void)usePic:(UIImage *)picture
{
imageView.image = picture;
}
#end
I came accross this issue recently too. My solution was to dismiss the modal UIImagePickerController view prior to using the image.
By dismissing the image picker, the previous view controller's view is reloaded, and the UIImageView which was to be the recipient of the chosen image is reloaded and no longer nil.
i.e. I changed from this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Wrong! imageView has been released by the UIViewController after a low memory warning and is now nil.
[imageView setImage:image];
[self dismissModalViewControllerAnimated:YES];
}
to this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Dismis the image picker first which causes the UIViewController to reload it's view (and therefore also imageView)
[self dismissModalViewControllerAnimated:YES];
[imageView setImage:image];
}
My guess: make sure you're handling didReceiveMemoryWarning correctly. Put a breakpoint or a debug printout or something to see if it's being hit, and then check what's happening next.
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
NSLog(#"Got a memory warning");
}
The default handling is for UIViewControllers that are not visible (and your app's main view controller is not visible while the picker is in front of it!) to discard their views.
I have had the same problem. When the imagePicker is opened there is a memory warning and the ViewController is discarted. So you lose anything in your ViewController. For example if you have any UITextField you will also lose its content. So you need to store the state of your ViewController somewhere else when the method didReceiveMemoryWarning is called. You can store values in the NSUserDefaults, in files, in your AppDelegate,...
Saving an image in a file is a big time consumption task. So my solution has been to save the image in the AppDelegate, in memory. I've created an UIImage *image variable inside the delegate. Then when the imagePicker finishes I store the image in that field:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
imageView.image = editedImage; // it works if no memory warning is received
YourAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
delegate.image = editedImage;
}
Then if the ViewController has been discarted it's needed to read the stored image from the AppDelegate in the viewDidLoad method.
- (void)viewDidLoad {
[super viewDidLoad];
// if you have used NSUserDefalts to store the state of your UITextFields
// when the didReceiveMemoryWarning was called
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *name = [defaults objectForKey:#"name"];
if (name != nil) {
textFieldName.text = name;
}
// show the saved image
YourAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
UIImage *image = delegate.image;
if (image != nil) {
imageView.image = image;
}
}
I've tried all these techniques and no joy. The image view does change shape to match the photo but the only thing that display's is the background. and if i attempt to take a photo the second time it works...
This doesn't make any sense whatsoever...could it be a problem with the XIB ??