I have a UIButton and when the user clicks it, I want to start the image picker controller
I already configure what I think it is enough but I couldn't know what is the message that I have to pass to that image picker to start working
This is my code
#import "ImagePickerViewController.h"
#interface ImagePickerViewController ()
#property (weak, nonatomic) IBOutlet UIImageView *image;
#end
#implementation ImagePickerViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)SelectImage:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
#end
I have already set the correct protocols:
#interface ImagePickerViewController : UIViewController < UINavigationControllerDelegate,UIImagePickerControllerDelegate>
#end
You need to present the UIImagePicker
- (IBAction)SelectImage:(id)sender
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePicker animated:NO completion:nil];
}
You can show the picker with:
[self presentViewController:imagePicker animated:YES completion:NULL];
And then in your delegate callback imagePickerController:didFinishPickingMediaWithInfo you get the image and dismiss the picker again:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = info[UIImagePickerControllerEditedImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
You have to tell your view controller class to show the UIImagePickerController.
Simply use
[self presentViewController:imagePicker animated:YES completion:nil];
This will present it modally. If you are running on an iPad, you instead have to present it in a popover controller, as follows:
Declare a new property in the #interface:
#property (nonatomic, strong) UIPopoverController* imagePickerPopover;
Present the popover controller e.g:
UIButton* button = sender;
self.imagePickerPopover = [[UIPopoverController alloc] initWithContentViewController:self.imagePicker];
[self.imagePickerPopover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
Related
I am trying to simply select an image from the iphone’s library and display it on the view. Here is the header file reference:
#interface myappViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
UIImagePickerController *imagepicker;
UIImagePickerController *imagepicker2;
UIImage *image;
IBOutlet UIImageView *imagepickerview;
}
- (IBAction)TakePhoto;
- (IBAction)ChooseExisting;
#end
Here is the implementation code:
#interface myappViewController ()
#end
#implementation myappViewController
- (IBAction)TakePhoto{
imagepicker = [[UIImagePickerController alloc] init];
imagepicker.delegate = self;
[imagepicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:imagepicker animated:YES completion:NULL];
}
- (IBAction)ChooseExisting{
imagepicker2 = [[UIImagePickerController alloc] init];
imagepicker2.delegate = self;
[imagepicker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagepicker2 animated:YES completion:NULL];
}
- (void) imagePickerController:(UIImagePickerController *)imagepicker didFinishPickingMediaWithInfo:(NSDictionary *)info{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imagepickerview setImage:image];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)imagepickerview{
[self dismissViewControllerAnimated:YES completion:NULL];
}
On the storyboard I have two buttons. One for choose existing and one for take photo. If I select the choose existing the library opens and I can select an image. The problem is that the image is not selected and then redirected back to the xcode view. Rather, I have to click the cancel button in the navigation controller of the photo library and then I see the image loaded in the view. Any idea why this is not properly loading?
Please use valueForKey instead of objectForKey also dismiss the image picker before getting the image.
[self dismissViewControllerAnimated:NO completion:nil];
image = [info valueForKey:UIImagePickerControllerOriginalImage];
[imagepickerview setImage:image];
Im trying to be able to take and capture a photo and show it in a uiimage view AND/OR take a capture a video and play it back in a movieplayercontroller.
I am able to click a button and "Take Photo" which does display in my uiimageview just fine. However the image disappears out of the uiimageview if i click a button to "Take Video" The take video works and displays back fine but like a said my uiimage has now disappeared.
If i go back to re take the photo the video i captured disappears?
.h file
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import <MobileCoreServices/MobileCoreServices.h>
#interface addEventViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
UIImagePickerController *imagePicker;
UIImage *eventPhoto;
IBOutlet UIImageView *imageView;
}
-(IBAction)TakePhoto;
- (IBAction)takeVideo:(id)sender;
#property (copy, nonatomic) NSURL *movieURL;
#property (strong, nonatomic) MPMoviePlayerController *movieController;
#end
.m file
#import "addEventViewController.h"
#interface addEventViewController (){
}
#end
#implementation addEventViewController
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
// FOR THE TAKE PHOTO
eventPhoto = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setImage: eventPhoto];
[self dismissViewControllerAnimated:YES completion:NULL];
// FOR THE TAKE VIDEO
self.movieURL = info[UIImagePickerControllerMediaURL];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
//FOR THE TAKE PHOTO
[self dismissViewControllerAnimated:YES completion:NULL];
// FOR THE TAKE VIDEO
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
// TAKE PHOTO BUTTON
-(IBAction)TakePhoto{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
[imagePicker setSourceType: UIImagePickerControllerSourceTypeCamera];
[self presentViewController: imagePicker animated:YES completion:NULL];
}
// TAKE VIDEO BUTTON
- (IBAction)takeVideo:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)viewDidAppear:(BOOL)animated {
self.movieController = [[MPMoviePlayerController alloc] init];
[self.movieController setContentURL:self.movieURL];
[self.movieController.view setFrame:CGRectMake (420, 76, 320, 200)];
[self.view addSubview:self.movieController.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.movieController];
[self.movieController play];
}
- (void)moviePlayBackDidFinish:(NSNotification *)notification {
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I haven't actually compiled and run your code, but looking at it, you're defining imagePicker in the class instance, whereas you're defining picker inside the TakePhoto method. My guess is that when you run through the process, the method instance is being destroyed once it completes.
Instead, try using a single imagePicker instance declared at the class level, set its properties differently depending on whether the user is loading an image or video, and set the selected media accordingly.
How can I call this method from my MainController.m class and pass the Image to my MainController
CameraController.m
- (void) openCamera {
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourcetype = UIImagePickerContrllerSourceTypeCamera;
[self presentViewContrller:picker animated:YES completion:nil];
}
I'm still a newbie in iOS development and is it possible to use a delegate?
Edit:CameraController doesn't have a nib.
In CameraController.h add these lines:
#protocol CameraControllerDelegate <NSObject>
- (void)didFinishCapturingImage:(UIImage*)image;
#end
and add a property in CameraController class (in CameraController.h)
#property (nonatomic, strong) id<CameraControllerDelegate> delegate;
In MainController.h, add protocol in interface implementation; like this:
#interface MainController : NSObject <CameraControllerDelegate>
before you present/push CameraController from your MainController.m add this line:
cameraController.delegate = self;
In MainController.m implement didFinishCapturingImage method.
- (void)didFinishCapturingImage:(UIImage*)image {
//your logic
}
in Image Picker Controller Delegate, add this line:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion: Nil];
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//this will send delegate callback to MainController
[self.delegate didFinishCapturingImage:image];
}
EDIT: Second Solution:
Make a property in CameraController.h to keep instance of MainController object:
#property (nonatomic, strong) MainController *mainController;
Assign self to CameraController's instance's property mainController where you use CameraController:
cameraController.mainController = self;
Edit you openCamera method:
- (void) openCamera {
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self.mainController; //important line
picker.sourcetype = UIImagePickerContrllerSourceTypeCamera;
[self.mainController presentViewContrller:picker animated:YES completion:nil];
}
and implement following method in MainController.m
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//your logic
}
Don't forget to include UIImagePickerControllerDelegate in MainController.h.
No need to create protocols and implement protocol methods..
Something strange is happening with my code.
Scenario: Open camera->Take picture->Pass the image taken to a viewcontroller(from a storyboard.
The thing is that my UIimage variable from the destination view is not available, not recognized!
My code
postviewController.h
#import <UIKit/UIKit.h>
#interface postAlertViewController : UIViewController
#property (nonatomic, retain) UIImage *cameraImage;
#end
postviewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *aImageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width,150 )];
aImageview.image=cameraImage;
[self.view addSubview:amageview];
}
Take picture and pass it to the view controller above
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:NULL];
ViewController *postView = [self.storyboard instantiateViewControllerWithIdentifier:#"postview"];
postView.cameraImage=(UIImage *)[info objectForKey:UIImagePickerControllerEditedImage];
[self.navigationController pushViewController:postView animated:YES];
}
The stroryboard ID is set to "postview".
What i am getting is:
Property 'cameraImage' not found on object of type 'ViewController *'
Why cameraImage is not available from the origin view although is declared in destination's .h file?
In another situation where i needed to pass a string between views, in same manner as above
[yourViewController setValue:mysc.title forKey:#"sendAlertTitle"];
worked ok.
Any help?
Thank you.
try this
postAlertViewController *postView = [self.storyboard instantiateViewControllerWithIdentifier:#"postview"];
and always use class and interface name start with capital
you can use this one for more ease.
- (IBAction)captureImage:(id)sender
{
if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *deviceNotFoundAlert = [[UIAlertView alloc] initWithTitle:#"No Device" message:#"Camera is not available"
delegate:nil
cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[deviceNotFoundAlert show];
} else {
UIImagePickerController *cameraPicker = [[UIImagePickerController alloc] init];
cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraPicker.delegate =self;
// Show image picker
[self presentViewController:cameraPicker animated:YES completion:nil];
}
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
UIImage *selectedImage =[info objectForKey:UIImagePickerControllerOriginalImage];
ViewControllerName *Vc=[self.storyboard instantiateViewControllerWithIdentifier:#"captioEdit"];
Vc.postImgStr=[self scaleAndRotateImage:selectedImage];
[picker dismissViewControllerAnimated:YES completion:nil];
[self.navigationController pushViewController:captioViewEdit animated:YES];
}
//Delegate method of UIImagePickerController for image picker model cancel
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
Thanks
I have two views. Very simple set up just to play around with the camera roll.
One view will bring up the camera roll using:
[self presentViewController:self.imagePicker animated:NO completion:nil];
which is triggered by a UIButton. It brings up the camera, everything's fine.
After taking a picture, I am calling the following to dismiss the controller:
[self dismissViewControllerAnimated:NO completion:nil];
Again, everything is working.
But the view that brings up the imagePicker Controller no longer receives touch (the UIButton will no longer brings up the camera again) after the Modal View is dismissed.
It will only start to receive touches again when I switch to another view and come back.
I have been searching for a solution to this problem but have not been successful in finding anything. Thanks in advance.
EDIT (Adding code):
In CameraController.m
This is where brings up the Camera Roll
(Subclass of UIViewController conforming to
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>)
//UIButton that brings up the imagePicker
- (IBAction)useCamera
{
[self prepareImagePicker];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
//Initializing ImagePicker
- (void)prepareImagePicker
{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage, nil];
self.imagePicker.delegate = self;
OverlayCameraView *cameraOverlay = [[OverlayCameraView alloc] initWithFrame:(CGRect){{0, 0}, 320, 480} andTheCameraController:self];
self.imagePicker.cameraOverlayView = cameraOverlay;
}
}
//Delegate Method when a picture is taken
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.imagePicker.delegate = nil;
[picker dismissViewControllerAnimated:NO completion:nil];
self.imagePicker = nil;
self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
In OverlayCameraView.m
//Camera Overlay Class (Subclass of UIView)
- (id)initWithFrame:(CGRect)frame andTheCameraController:(CameraController*)cameraController
{
if ((self = [super initWithFrame:frame]))
{
self.camera = cameraController;
//.…2 UIButtons set up
}
return self;
}
//Overlay Cancel Button
- (void) cancel
{
[self.camera dismissViewControllerAnimated:NO completion:nil];
}
//Overlay Take Picture Button
- (void) takeAPicture
{
[self.camera.imagePicker takePicture];
}
You should be calling dismissViewControllerAnimated on the picker not self via the UIImagePickerControllerDelegate methods.
[picker dismissViewControllerAnimated:NO completion:nil];
or if you are keeping a reference to it:
[self.imagePicker dismissViewControllerAnimated:NO completion:nil];
EDIT:
Based on the revised code, I believe
self.imagePicker.cameraOverlayView = cameraOverlay;
should be:
self.imagePicker.cameraOverlayView = cameraOverlay.view;
Also this will cause a +2 reference count and will leak if you are not using ARC:
self.imagePicker = [[UIImagePickerController alloc] init];
It should be for proper reference counting:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
self.imagePicker = picker;
[picker release];