1

Im newish to Objective-C and I want to create an app with that will eventually take pictures with the camera but with images overlaid. But I thought I would start with a simple camera app just to get the base for it. I have followed a tutorial for creating a app that you can take pictures with and access image library. BUT when I build it to my iPhone 4s it just loads a white screen. If any one could take a look at the code, but I think its right and offer an answer it would be great thanks.

//ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

<UIImagePickerControllerDelegate, UINavigationControllerDelegate>


@property (strong, nonatomic) IBOutlet UIImageView *imageView;

- (IBAction)takePhoto:(UIButton *)sender;

- (IBAction)selectPhoto:(UIButton *)sender;


@end

//ViewController.m

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                              message:@"Device has no camera"
                                                             delegate:nil
                                                    cancelButtonTitle:@"OK"
                                                    otherButtonTitles: nil];

        [myAlertView show];

    }

}

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];

}

- (IBAction)takePhoto:(UIButton *)sender {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:picker animated:YES completion:NULL];

}

- (IBAction)selectPhoto:(UIButton *)sender {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picker animated:YES completion:NULL];


}

#pragma mark - Image Picker Controller delegate methods

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;

    [picker dismissViewControllerAnimated:YES completion:NULL];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissViewControllerAnimated:YES completion:NULL];

}

@end
CaptJak
  • 3,592
  • 1
  • 29
  • 50
MrHolroyd
  • 51
  • 1
  • 1
  • 7
  • Where is the white screen? Is it the only thing you see? Do you see your buttons? – CaptJak Sep 05 '13 at 14:38
  • in your `viewDidLoad` add this: `[self.view setBackgroundColor:[UIColor redColor]];` then compile. If you see a background color of red instead of white, that means you're not added the camera view to the subview of your viewController. – John Riselvato Sep 05 '13 at 14:41
  • No i dont see the buttons either its loads straight into a white screen. – MrHolroyd Sep 05 '13 at 14:42
  • @JohnRiselvato I got the red screen, how do I add it to the subview? – MrHolroyd Sep 05 '13 at 14:44
  • Do you have any other ViewControllers in IB that you are not using? Check to make sure that the VC with the button is set to the initial ViewController. – CaptJak Sep 05 '13 at 14:44
  • @CaptJak I have no other VC's only Appdelegate.h/m – MrHolroyd Sep 05 '13 at 14:49
  • If you only have one, then where are your buttons? Do you have any ViewControllers in the Interface Builder? Do you have any xibs? – CaptJak Sep 05 '13 at 14:52
  • I know AppDel isnt a VC. No other VC. But I am still getting the red screen after adding what John suggested. But im unsure of how to add it to the subview. – MrHolroyd Sep 05 '13 at 14:54
  • What I suspect is that the buttons were never added to the VC in the xib file, therefore, you get a white screen. The reason you get a red screen is because you set the background color for the VC in code, so whether there is a VC in IB or not, you will get a red background either way. You don't create your buttons programmatically, so you get no buttons. – CaptJak Sep 05 '13 at 14:57
  • I'm not sure what you mean, I created the button in the .xib using the interface builder. – MrHolroyd Sep 05 '13 at 15:01
  • Strange... Try changing the background color of the VC in the xib file (try green) and see if that changes. I followed the tutorial you just did ([AppCoda](http://www.appcoda.com/ios-programming-camera-iphone-app/), right?) and it worked for me – CaptJak Sep 05 '13 at 15:03
  • Yea thats the tutorial i followed. Changed it to green and now when I run it get a green background. – MrHolroyd Sep 05 '13 at 15:07
  • And you still see no buttons? That makes no sense. Are your buttons set to hidden? – CaptJak Sep 05 '13 at 15:09
  • Lets try programmatically adding a button to your viewController. Then make the selector your `takePhoto:` function. http://stackoverflow.com/questions/1378765/how-do-i-create-a-basic-uibutton-programmatically – John Riselvato Sep 05 '13 at 15:11
  • No, no buttons when I run it. But they are there in the .xib. http://postimg.org/image/5pp3xkhvr/ – MrHolroyd Sep 05 '13 at 15:11
  • Im getting an error, havn't made buttons programmatically yet. Is this correct? UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside]; [button setTitle:@"take photo" forState:UIControlStateNormal]; button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); [ViewController addSubview:button]; – MrHolroyd Sep 05 '13 at 15:21
  • Almost. Change `(aMethod)`, to `(takePhoto)`. – CaptJak Sep 05 '13 at 15:26
  • Ok changed them, just built it again and still getting the same green background with no button. – MrHolroyd Sep 05 '13 at 15:34
  • Without sitting at your computer, I can only say do it over. Trash the project, start anew. Honestly you could have made 3 camera apps in the time that this question has been open. – CaptJak Sep 05 '13 at 17:21
  • I think that your UIButton is not connected to the IBAction. Try reconnecting it. Please respond if this helps. – Abdullah Shafique Sep 05 '13 at 14:28
  • Hi Abdullah, I just double checked it and they are connected. – MrHolroyd Sep 05 '13 at 14:40
  • @MrHolroyd Do you get a white screen when the button is pressed? – Abdullah Shafique Sep 05 '13 at 14:48

0 Answers0