I declared this in my header:
#import <UIKit/UIKit.h>
#interface NFNoteCamera : UIImagePickerController
#end
and receive 27 semantic issues such as
Property 'cameraCaptureMode' requires method 'cameraCaptureMode' to be defined - use #synthesize, #dynamic or provide a method implementation in this class implementation
including 'allowsImageEditing', 'allowsEditing' and other camera featured issues. If i had to guess it was something i haven't imported yet. Any Ideas?
as the doc says
Important The UIImagePickerController class supports portrait mode
only. This class is intended to be used as-is and does not support
subclassing. The view hierarchy for this class is private and must not
be modified, with one exception. In iOS 3.1 and later, you can assign
a custom view to the cameraOverlayView property and use that view to
present additional information or manage the interactions between the
camera interface and your code
why dont you just implement its delegate?
#interface NFNoteCamera : ParentClass <UIImagePickerControllerDelegate>
{
UIImagePickerController *yourPicker;
}
#end
#implementation NFNoteCamera
-(void)anyMethod{
yourPicker = [[UIImagePickerController alloc] init];
yourPicker.delegate = self;
[yourPicker setAllowsEditing:BOOL];
//or photo library(UIImagePickerControllerSourceTypePhotoLibrary)
yourPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:yourPicker animated:YES];
}
//delegate methods
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *producedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
}
#end
Related
Please note that the answer below - do not work for iOS6 so I still need an answer!
My application is enabled only for Portrait mode.
However, if I embed a UIImagePickerController inside as a subview, and rotate the device, the top and bottom bar stays in the same location, however UIImagePickerController does rotate.
How can I prevent it from rotating?
This is the code:
[self.view.window addSubview:self.imagePickerController.view];
self.imagePickerController.showsCameraControls = NO;
self.imagePickerController.view.frame = CGRectMake(0, 90, 320, 320);
self.imagePickerController.allowsEditing = NO;
EDITED
I am using iOS6 where shouldAutorotate is not being calle
Add this UIImagePickerController category in your class,
#interface UIImagePickerController(Nonrotating)
- (BOOL)shouldAutorotate;
#end
#implementation UIImagePickerController(Nonrotating)
- (BOOL)shouldAutorotate {
return NO;
}
#end
include the following in your controller this will work, I'm just creating the category of UIImagePickerController
#interface UIImagePickerController (private)
- (BOOL)shouldAutorotate;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;
- (NSUInteger)supportedInterfaceOrientations;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
#end
#implementation UIImagePickerController (Private)
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate {
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
One possibility is to override the
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
method of UIImagePickerController. I'm not sure if this is the best possibility but it will work.
So if you only want your UIImagePickerController to be rotated to portrait use the following code
#interface PortraitUIImagePickerController : UIImagePickerController
#end
And the implementation should look like the following
#implementation PortraitUIImagePickerController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}
#end
The category in the most voted answer works, but since it is discouraged to use categories, you can also make a subclass of UIImagePickerController and use that.
If you want to avoid rotating of the UIImagePickerController add the following class
UINonRotatableImagePickerController.h
#interface UINonRotatableImagePickerController : UIImagePickerController
#end
UINonRotatableImagePickerController.m
#implementation UINonRotatableImagePickerController
- (BOOL)shouldAutorotate
{
return NO;
}
#end
You have to change the UIImagePicker class in the storyboard to use UILandscapeImagePickerController, or if you allocate it in code, change
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
to
UIImagePickerController *picker = [[UINonRotatableImagePickerController alloc] init];
and include UINonRotatableImagePickerController.h in your code.
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).
I am writing an app that has an initial view containing 2 buttons - one that allows the user to take a photo with the camera and the other that allows him to select a picture from the library.
I've written the code that allows that to happen, but after selecting the picture, I want to go to another view that allows, say, sharing the picture or whatever. Could anyone tell me how to do something like "whenPhotoIsSelected, view = newView"?
This is my code so far:
#pragma mark -
-(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];
}
I am aware of the existence of
-(void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
but how exactly do I use it? I tried it and it crashes...
Implement the UIImagePickerControllerDelegate in your ViewController.
To do that add <UIImagePickerControllerDelegate> to your ViewController's interface declaration (in the .h file) like so:
#interface YourViewController : UIViewController <UIImagePickerControllerDelegate> {
// instance variable declarations etc.
}
Then in your ViewController's .m file you actually implement the method -(void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo instead of calling it, like so:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
// add the new view, for example you could push it on your navigation stack if you use a UINavigationController
}
That method will be called by the UIImagePicker when an image was selected.
I have a camera controller based on a UIImagePickerController:
#interface CameraController : UIImagePickerController
<UIImagePickerControllerDelegate>
// <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
}
#end
Init generates a warning that UINavigationControllerDelegate is not implemented (it is expected since I don't want my object to be that delegate):
- (id) init
{
if(!(self = [super init]))
return nil;
super.sourceType = UIImagePickerControllerSourceTypeCamera;
// Commenting delegat does not help
super.delegate = self;
return self;
}
Despite being a UIImagePickerControllerDelegate delegate, imagePickerController:didFinishPickingMediaWithInfo: is not called. I also verified the other two delegate methods are not being called either.
If I do claim adherence to UINavigationControllerDelegate, imagePickerController:didFinishPickingMediaWith is called but I crash on dismissModalViewControllerAnimated:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
ASSERT_VALID(picker);
ASSERT_VALID(info);
// Commenting does not help
[picker autorelease];
UIImage* image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
ASSERT_VALID(image);
// Image processing commented out
// super.delegate = nil;
// picker.delegate = nil;
// [self dismissModalViewControllerAnimated:YES];
// [super dismissModalViewControllerAnimated:YES];
// [self performSelector:#selector(dismissModalViewControllerAnimated:) withObject:nil afterDelay:0.0f];
// [self dismissModalViewControllerAnimated:NO];
// [super dismissModalViewControllerAnimated:NO];
[self performSelector:#selector(dismissModalViewControllerAnimated:) withObject:nil afterDelay:0.5f];
}
I've looked at a number of posts on this CURSED view controller. The closest, UIImagePickerControllerDelegate not responding properly and iPhone - UIImagePickerControllerDelegate inheritance, did not help.
Any ideas what might be this time? Two questions immediately come to mind: (1) what does being a UINavigationControllerDelegate have to do with invoking my callback with the image, and (2) why can't this object clean itself up properly?
Thanks in advance,
The UIImagePickerController class is not supposed to be subclassed. The reference manual says:
This class is intended to be used
as-is and does not support
subclassing.
And there's no reason to. Just create a subclass of a UIViewController and implement the delegates.
I've successfully used it many times. I've always declared that my view controller implements UINavigationControllerDelegate but haven't implemented any of the methods of this protocol.
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 ??