My app run on landscape mode only.
I read about this and say UIImagePickerController load only portrait mode.
How can rotate the app to pick image and return to landscape mode again.
I have this code and crashes my app.
- (IBAction)LoadImage:(id)sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate=self;
[picker setSourceType:(UIImagePickerControllerSourceTypePhotoLibrary)];
[self presentViewController:picker animated:YES completion:Nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:NO completion:nil];
//.....
}
I try this but not work...
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return (UIInterfaceOrientationMaskAll);
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskPortrait);
}
IN your code
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[##self## dismissViewControllerAnimated:NO completion:nil];
//.....
}
self## should be replaced with ##picker## if you want to dismiss the picker
You just have to navigate again to your current view it works for me
Related
how do I set a UICollectionViewCell to be used as a button , that also repeats itself when the function is done.
I want to use the cell as a button to add images from my photo library.
The code extract is the function of the barbutton item I used to add images.
- (IBAction)buttonTapped:(id)sender {
_picker = [[UIImagePickerController alloc] init];
_picker.delegate = self;
_picker.allowsEditing = NO;
_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:_picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[_images addObject:image];
[self saveNewImageToDb:image];
[self dismissViewControllerAnimated:_picker completion:^{
[_collectionView reloadData];
}];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:_picker completion:nil];
}
You can implement this function :
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// something you do as function "buttonTapped"
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Selected cell = %d",indexPath.item);
_picker = [[UIImagePickerController alloc] init];
_picker.delegate = self;
_picker.allowsEditing = NO;
_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:_picker animated:YES completion:nil];
}
I have two UI Buttons in my view controller, and an imageview. One button has the user pick a picture from the photo library and add it to the UIImage. The other has the user take a new picture and show it in the UIimage.
The libraryButton and actions all work, however when I add the cameraButton code I get an error since I'm using the same "startcameracontrollerfromviewcontroller" My question is can I simply change this to startcameracontrolfromviewcontroller? or is there something else I would have to change.
#pragma mark - Button Actions
- (IBAction)libraryButton:(id)sender {
[self startCameraControllerFromViewController:self usingDelegate:self];
}
#pragma mark - UIImagePickerControllerDelegate methods
- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>) delegate {
if (([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera] == NO) || (delegate == nil) || (controller == nil))
return NO;
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
cameraUI.delegate = delegate;
[controller presentViewController:cameraUI animated:YES completion:nil];
return YES;
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage * image = info[UIImagePickerControllerOriginalImage];
_imageView.image = image;
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - Camera Button
- (IBAction)cameraButton:(id)sender {
[self startCameraControllerFromViewController:self usingDelegate:self];
}
#pragma mark - Camera Delegate methods
- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>) delegate {
if (([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera] == NO) || (delegate == nil) || (controller == nil))
return NO;
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.delegate = delegate;
[controller presentViewController:cameraUI animated:YES completion:nil];
return YES;
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage * image = info[UIImagePickerControllerOriginalImage];
_imageView.image = image;
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
You can call the same method from each IBAction passing it a different value that will be used to determine the image pickers source type - (for this example I'm not passing in the presenting view controller or delegate, but this could be easily adapted to implement)
- (IBAction)libraryButton:(id)sender {
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
- (IBAction)cameraButton:(id)sender {
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypeCamera];
}
- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType {
// start config here....
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
// set the source type here
imagePickerController.sourceType = sourceType;
// do any extra setting up... present controller...
}
I suggest looking at this sample code provided by Apple - Using UIImagePickerController to Select Pictures and Take Photos
I am trying to have a button that is meant to open the camera and a button that is meant to open camera roll. Here is my code:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)getCameraPicture:(id)sender{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = NO;
picker.sourceType = (sender == takePictureButton) ?
UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
- (IBAction)selectExistingPicture {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry" message:#"Device does not support photo library" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
}
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
imageView.image = image;
}
- (void)viewDidLoad
{
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
takePictureButton.hidden = YES;
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Any ideas on why it's not working? Everything is linked but the buttons do nothing
Thanks
EDIT
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIImagePickerControllerDelegate> {
IBOutlet UIButton *takePictureButton;
IBOutlet UIImageView *imageView;
}
#property (nonatomic, retain) IBOutlet UIImageView *imageView;
#property (nonatomic, retain) IBOutlet UIButton *takePictureButton;
- (IBAction)getCameraPicture:(id)sender;
- (IBAction)selectExistingPicture;
#end
Just add :
[self presentViewController:picker animated:YES completion:nil];
at the end of your 'getCameraPicture:' and in your 'selectExistingPicture:'.
And you also might want to add the 'UIImagePickerControllerDelegate' to your view controller's interface so the 'didFinishPickingImage' gets called.
Edit :
You should replace your :
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo;
with
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
beccause that method is long deprecated.
Use it like this :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Code from http://www.appcoda.com/ios-programming-camera-iphone-app/.
-(IBAction)TakePhoto {
picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentedViewController:picker animated:YES completion:nil];
}
- (IBAction)ChooseExisting {
picker2 = [[UIImagePickerController alloc]init];
picker2.delegate = self;
[picker2 setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentedViewController:picker2 animated:YES completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageview setImage:image];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:NULL];
When I try to run this app I the
[self presentedViewController:picker animated:YES completion:nil];
Gets highlighted in red saying No visible #interface for ViewController Declares the sector presentedviewcontroller:animated :completion :
My .h file is
#interface ViewController : UIViewController <UIImagePickerControllerDelegate,
UINavigationControllerDelegate> {
UIImagePickerController *picker;
UIImagePickerController *picker2;
UIImage *image;
IBOutlet UIImageView *imageview;
}
- (IBAction)TakePhoto;
- (IBAction)ChooseExisting;
You have a typo: presentedViewController instead of presentViewController on the line
[self presentedViewController:picker animated:YES completion:nil];
(in more than one place)
How do I save a UIImagePickerController camera image so I can send to CreateViewController scene's UIImageView?
HomeViewController(scene 1) has a button that loads UIImagePickerController and returns with an image from the camera. CreateViewController(scene 2) has an empty UIImageView.
HomeViewController.h
#import "CreateViewController.h"
#interface HomeViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
#property (strong, nonatomic) UIImagePickerController *imagePicker;
#property(nonatomic, retain) UIImage *myImage;
- (IBAction)cameraImage:(id)sender;
HomeViewController.m
#implementation HomeViewController
#synthesize imagePicker, myImage;
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ( [segue.identifier isEqualToString:#"create"]) {
CreateViewController *cvc = [segue destinationViewController];
UIImage *image = myImage;
cvc.myImage = image;
}
}
//Camera button action
- (IBAction)cameraImage:(id)sender{
//UIImagePickerController space in memory
imagePicker = [[UIImagePickerController alloc]init];
//Set the delegate
imagePicker.delegate = self;
//Set the sourceType
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
//Show Image Picker UI
[self presentViewController:imagePicker animated:YES completion:^{}];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info {
self.myImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:YES completion:^{}];
[self performSegueWithIdentifier:#"create" sender:self];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:^{}];
}
CreateViewController.h
#property (strong, nonatomic) IBOutlet UIImageView *bgImage;
#property(nonatomic, retain) UIImage *myImage;
CreateViewController.m
#implementation CreateViewController
#synthesize bgImage, myImage;
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = myImage;
[bgImage setImage:image];
}
Download sample code here; http://code-blind.com/ios6-camera-picture-to-another-scenes-uiimageview/
Using a segue is not mandatory if you are instanciating the controller programmatically.
The easiest way to achieve that is something like
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage * pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
DisplayViewController * controller = [DisplayViewController new];
controller.imageView.image = pickedImage;
[self.navigationController pushViewController:controller animated:YES];
}
If you want to use a segue you need to manually invoke it by doing
[self performSegueWithIdentifier:#"your-segue-name" sender:self];
then you can use an ivar in your HomeViewController to store the pickedImage and pass it to your destinationViewController.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
DisplayViewController * controller = (DisplayViewController *)segue.destinationViewController;
controller.imageView.image = _pickedImage;
}
where _pickedImage is the ivar where you stored the image after it has been picked.
On ur first view
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
CreateViewController* sI = [self.storyboard instantiateViewControllerWithIdentifier:#"XXXXX"];
sI.myImage = image;
}
XXXXX is identifier of view.
First you have to be sure that your delegate method is called and that you have the image.
Next, store the image to a property:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
self.takenImage = image; // assuming you have declared #property(nonatomic, strong) UIImage *takenImage;
[picker dismissViewControllerAnimated:YES completion:^{
[self performSegueWithIdentifier:#"my-segue" sender:self];
}];
}
After that, this method's being called:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// you need to set the picked image to the controller:
DisplayViewController *controller; // get the controller
controller.image = self.takenImage;
}
in this code:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
if( i ==1)
{
[self dismissViewControllerAnimated:YES completion:^
{
Second_ViewController*secondview=[[UIStoryboard storyboardWithName:#"Main" bundle:nil]instantiateViewControllerWithIdentifier:#"Second_ViewController"];
secondview.bb_image=image;
[[NSUserDefaults standardUserDefaults ]setInteger:0 forKey:#"123"];
[[self navigationController] pushViewController:secondview animated:YES];
}];
}