Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclassing UIImagePickerController for adding functionality

So basically what i'm trying to do is, change the button of "take image", to a custom button of mine. And when the user will change DeviceOrientation, the button will change according to the orientation. I've already tried using the "overLay" property, but it seems not possible to change or move the "overLay" once its placed. So I've decided to subclass the UIImagePickerController, but when i call it to present, the camera will not display at all!

Any help at understanding what is the problem will help... Thanks!!

This is the viewController Calling subclasses "UIImagePickerController":

@interface CameraViewController : UIViewController<UIImagePickerControllerDelegate>
@interface CameraViewController ()
@property (nonatomic) pickerControllerMC *cameraPickerView;

self.cameraPickerView = [[pickerControllerMC alloc]initWithNibName:@"pickerControllerMC"       bundle:nil];
self.cameraPickerView.delegate = self;


// Do any additional setup after loading the view.
//create a new image picker instance
//set source to video!
self.cameraPickerView.sourceType = UIImagePickerControllerSourceTypeCamera;
//hide all controls
self.cameraPickerView.showsCameraControls = NO;
self.cameraPickerView.navigationBarHidden = YES;
self.cameraPickerView.toolbarHidden = YES;
//  self.delegate = self;
//make the video preview full size
self.cameraPickerView.wantsFullScreenLayout = YES;
self.cameraPickerView.cameraViewTransform =
CGAffineTransformScale(self.cameraPickerView.cameraViewTransform,
                       CAMERA_TRANSFORM_X,
                       CAMERA_TRANSFORM_Y);

//Getting WidthAnd hight
CGRect screen = [[UIScreen mainScreen] bounds];
//CGFloat widthInPixel = screen.size.width * scaleFactor;
//CGFloat heightInPixel = screen.size.height * scaleFactor;
int Width = screen.size.width;
int hight = screen.size.height;

[self presentViewController:self.cameraPickerView animated:YES completion:nil];

And this is The subclass:

 @interface pickerControllerMC : UIImagePickerController

 @implementation pickerControllerMC

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
    }

    - (void)viewDidLoad
     {
    [super viewDidLoad];

    UIButton *buttonTakeImage = [UIButton
                        buttonWithType:UIButtonTypeRoundedRect];
    [buttonTakeImage setBackgroundImage:[UIImage imageNamed:@"CameraButton@2x"]     forState:UIControlStateNormal];

    [buttonTakeImage addTarget:self action:@selector(takePictureClicked)     forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.overlay];
    [self.view addSubview:buttonTakeImage];


    //Adding orientation Notifications!
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver: self selector:        @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];


    }

Edit

Ok my mistake was, i used "initWithNibName", so i got a white Nib screen every time.. all good.

like image 262
user2904350 Avatar asked Jan 11 '14 12:01

user2904350


People also ask

What is kUTTypeImage?

kUTTypeImage is actually default for the mediaTypes property. It states, that one can only pick still images. If you are ok with this default, you don't need to set it explicitly in your code.


1 Answers

Sorry, you cannot subclass UIImagePickerController. From the docs:

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

However, you can have complete control over the picture taking UI.

  • You can provide a cameraOverlayView to a UIImagePickerController.

  • Avoid UIImagePickerController entirely by using AVFoundation classes to implement picture taking and your own UIViewController subclasses as the controller and views managing the UI and behavior. See https://developer.apple.com/library/mac/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/04_MediaCapture.html.

like image 55
XJones Avatar answered Oct 05 '22 23:10

XJones