How can I create the image picker in code?
I use iOS 6.0 , with ARC, for ipad.
I would like to able to select the picture and somehow get UIImage of the selected image.
i did add delegates:
enter code here
in the viewDidLoad method did
enter code hereimagePicker = [[UIImagePickerController alloc] init];
and in the button method for click i have put
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
the crash happens in
[self presentModalViewController:imagePicker animated:YES];
There are some tutorials here http://www.raywenderlich.com/130/how-to-write-a-custom-image-picker-like-uiimagepicker and http://mobileorchard.com/ios-advanced-programming-the-image-picker/ that might help you
I have similar code in one of my projects, but I had
[self presentViewController:imagePicker animated:YES completion:nil];
instead of
[self presentModalViewController:imagePicker animated:YES];
Try that and see if it works. Note the presentViewController rather than presentModalViewController.
Add a property in .h
#property (strong) UIPopoverController *pop;
In you r .m file under your button implimentation add something like:
if (self.pop) {
[self.pop dismissPopoverAnimated:YES];
}
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES; //if you want to edit the image
self.pop=[[UIPopoverController alloc] initWithContentViewController:imagePickerController];
//choose the direction of the arrow for the popover
[self.pop presentPopoverFromRect:((UIButton *)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
Make sure you have your <UIPopoverControllerDelegate> delegate set in .h
Related
I have a problem showing the album a toolbar with the following buttons, previous and the new one appears in the bottom view. How can you hide this toolbar?
Thanks in advance.
Album photo image
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
controller.allowsEditing = YES;
controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypePhotoLibrary];
controller.delegate = self;
[self.navigationController presentViewController: controller animated: YES completion: nil];
If you want to hide the toolbar you can use the following code . Here the toolBarName is the name of your toolbar.
toolBarName.hidden=YES;
Include this in the viewWillAppear function:
self.navigationController?.isToolbarHidden = true
However, this is a Swift solution.
Use this code.Hope it helps..
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init] ;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
imagePickerController.delegate = self;
imagePickerController.allowsEditing = FALSE;
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController animated:YES completion:nil];
}
I'm new to iOS coding, so my apologies in advance. I know this topic has been covered many times (I searched) however I can't seem to resolve my problem, which is why I am posting.
I am trying to access the iOS photo gallery but I keep getting two errors:
One is
'Application tried to present a nil modal view controller on target .'
Edit: The above error was fixed by initing the _picker in ChooseExsisting, as was suggested in the comments.
The other
[CameraController ChooseExsiting:]: unrecognized selector sent to instance 0x157e11330'
My code is as follows:
- (IBAction)ChooseExsiting {
UIImagePickerController *pickerController = [[UIImagePickerController alloc]
init];
pickerController.delegate = self;
[self.picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:_picker animated:YES completion:NULL]; }
I imagine my ChooseExsisting code is incorrect. Would anyone have any suggestions? I would greatly appreciate it.
In your ChooseExisting method, you instantiate a controller into a local variable, but then call present with _picker property variable which is probably nil. Either present the controller from the local variable or init the property like in TakePhoto method.
EDIT: As for the second part, both your IBActions have a wrong method signature if you are connecting them to Tap handlers in the Storyboard for example. They should look like this:
- (IBAction) TakePhoto:(id)sender
- (IBAction) ChooseExsiting:(id)sender
Change your ChooseExisitng method to below one:
- (IBAction)ChooseExsiting:(id) sender {
UIImagePickerController *pickerController = [[UIImagePickerController alloc]
init];
pickerController.delegate = self;
[pickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:pickerController animated:YES completion:NULL]; }
or
- (IBAction)ChooseExsiting:(id) sender {
self.picker = [[UIImagePickerController alloc]
init];
self.picker.delegate = self;
[self.picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:self.picker animated:YES completion:NULL]; }
You can try this . It work for me
Follow below three steps1.First of all you need to add below delegate in .h file (UIImagePickerControllerDelegate,UIPickerViewDelegate)
2.Add below three methods in your controller
3. On button click call below methods
here choose photo for open gallary. so on button click call choose photo method
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
CGSize newSize=CGSizeMake(150, 150);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[[info objectForKey:UIImagePickerControllerEditedImage] drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
img_profile_pic.image = newImage;
[self dismissViewControllerAnimated:YES completion:nil];
isProfilePic=TRUE;
}
- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.allowsEditing = YES;
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerController.sourceType = sourceType;
imagePickerController.delegate = self;
imagePickerController = imagePickerController;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void) takePhoto{
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypeCamera];
}
-(void) choosePhoto{
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
Instead of doing
if ([UIImagePickerController isSourceTypeAvailable:!UIImagePickerControllerSourceTypeCamera]) {
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
[self.picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:_picker animated:YES completion:NULL];
} }
You should do this
if ([UIImagePickerController isSourceTypeAvailable:!UIImagePickerControllerSourceTypeCamera]) {
if(!_picker){
_picker = [[UIImagePickerController alloc] init];
_picker.delegate = self;
[_picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:_picker animated:YES completion:NULL];
}else{
[self presentViewController:_picker animated:YES completion:NULL];
}}}
You should initialize _picker property before presenting it, right now you're trying to initialize local property of VC or you can also present self.picker instead of _picker in your code.
I am using UIImagePickerviewController to open the photo library through the below attached code... after called bit lines of code. The application was crashed... Its working fine in ios5
UIImagePickerController* content = [[UIImagePickerController alloc] init];
content.delegate = self;
content.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:content animated:YES];
[content release];
Is anything wrong with this code?
Check Crash on presenting UIImagePickerController under ios6 You will get everything you need to make UIImagePickerviewController working on iOS 6.0.
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[popover presentPopoverFromRect:cameraButton.frame inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
else{
[self presentModalViewController:imagePicker animated:YES];
}
I had the same issue. Because the UIImagePicker shows on portrait mode.
I fixed it by subclassing the UIImagePicker and implementing the shouldAutorotate method like:
- (BOOL)shouldAutorotate
{
return NO;
}
I have created instance of my subclassed imagePicker instead of UIImagePicker, everything worked fine. Hope this will help you.
I am trying to write an app that will allow the user to select an image from the photo library on the iPad. I have done exactly as sources online say it should be done, however when I click the button only the arrow is showed on screen and nothing else. The popover which should show with the images does not show. Could anyone please tell me why this is happening? Here is my code:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
[picker setDelegate:self];
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[picker setAllowsEditing:YES];
popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
[popoverController presentPopoverFromRect:self.view.frame inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
I know it is to to late to answer but if you're already interested on it, this may work for you:
In the .h file, gives this delegates:
#interface ViewController : UIViewController <UIPopoverControllerDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>
create the popover as a property:
#property (nonatomic, strong) UIPopoverController *popoverController;
and finally in the .m file:
-(IBAction)showPhotoLibray:(id)sender
{
BOOL hasGellery = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = hasGellery ? UIImagePickerControllerSourceTypePhotoLibrary : UIImagePickerControllerSourceTypePhotoLibrary;
if (self.popoverController != nil)
{
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController=nil;
}
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
CGRect popoverRect = [self.view convertRect:[self.theViewObject frame]
fromView:[self.theViewObject superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 300) ;
popoverRect.origin.x = popoverRect.origin.x;
[self.popoverController
presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
the self.theViewObject is an outlet of the object view controller that is calling the method, for example an UIButton
I have seen other posts on this subject, but no valid solutions. Surely this is possible! I found one solution here that suggests presenting it from a container view controller. The code for that is commented out in my method below. This DOES create the fullscreen view, but the cancel/take photo buttons won't work then, and I can't seem to dismiss it properly. Is there really no simple elegant solution to this???? Please help! Here's my code:
-(IBAction)launchCamera:(id)sender
{
[self.popoverController dismissPopoverAnimated:YES];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
//fullScreenViewController = [[UIViewController alloc] init];
//fullScreenViewController.contentSizeForViewInPopover = CGSizeMake(768, 1024);
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// Tried making the view full screen (or at least larger), but doesn't work...
//popoverController.contentViewController.contentSizeForViewInPopover = CGSizeMake(384, 512);
[imagePicker setTitle:#"camera"];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
imagePicker.delegate = self;
imagePicker.allowsEditing = NO;
//[fullScreenViewController.view addSubview:imagePicker.view];
// change imagePicker to fullScreenViewController here for full screen:
popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[popoverController setDelegate:self];
[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
I never could make the UIImagePicker work properly in full-screen, so I ended up using the AVFoundation framework to implement my own.