0

So I've had a few different problems with this app, its my first app and never coded before, first was I couldn't get my button to open a "secondviewcontroller" to display my image, thinks i fixed that now, now when my button loads my "secondviewcontroller" it opens the camera roll I select an image and then it closes and reopens the camera roll to reselect another image.

This is my FirstViewController.h - its calledBESPOKEViewController.h

#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>

@interface BESPOKEViewController : UIViewController
<UIImagePickerControllerDelegate,
UINavigationControllerDelegate>

@property BOOL newMedia;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
- (void)useCamera:(id)sender;
- (void)useCameraRoll:(id)sender;

@end

BESPOKEViewController.m

#import "BESPOKEViewController.h"


@interface BESPOKEViewController ()

@end

@implementation BESPOKEViewController





- (void)viewDidLoad
{
[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.
}



- (IBAction) useCamera:(id)sender
{
if ([UIImagePickerController isSourceTypeAvailable:
     UIImagePickerControllerSourceTypeCamera])
{
    UIImagePickerController *imagePicker =
    [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType =
    UIImagePickerControllerSourceTypeCamera;
    imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
    imagePicker.allowsEditing = NO;
    [self presentViewController:imagePicker
                       animated:YES completion:nil];
    _newMedia = YES;
}
}
- (IBAction) useCameraRoll:(id)sender{}





@end

This is my SecondViewController.h - its called secondpage.h

@interface secondpage : UIViewController
<UIImagePickerControllerDelegate,
UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end

secondpage.m

#import "secondpage.h"

@interface secondpage ()

@end

  @implementation secondpage

- (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.
}
- (void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
// method call
[self openGallery];

}

-(void) openGallery {
UIImagePickerController *imagePicker;

imagePicker = [[UIImagePickerController alloc] init];



imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;



imagePicker.delegate = self;

[self presentViewController:imagePicker animated:NO completion:^{



}];

}


- (void)imagePickerController:(UIImagePickerController *)imagePicker    didFinishPickingMediaWithInfo:(NSDictionary *)info

{

[self dismissViewControllerAnimated:YES completion:^{

}];
// change the name of imageview here your way
self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;

}


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

[picker dismissViewControllerAnimated:YES completion:NULL];
}



- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

updated to all code just not source from storyboard, would Thank you in advance

C.Wetherell
  • 215
  • 2
  • 14

2 Answers2

2

Because you're calling the [self openGallery]; method from viewDidAppear, openGallery will get called every time the view appears.

What is happening is:

SecondPage loads and then appears -> GalleryView Displayed -> Image selected -> Gallery view disappears -> SecondPage appears

When you dismiss the gallery view, viewDidAppear is called on SecondPage again, which is why you get a never ending loop.

This is part of the viewController lifecycle. Because only one viewController is being presented at a time, even though SecondPage was the view that was presenting the picker, it calls viewDidAppear again.

So what you need to do is call the openGallery method when the ViewController is first loaded, not every time it's displayed.

You could try putting the [self openGallery] call in viewDidLoad, or else have some logic to determine whether you want the gallery to be displayed in viewDidAppear or not.

This question also has a good answer to understand the viewController life-cycle: Looking to understand the iOS UIViewController lifecycle

Community
  • 1
  • 1
Matt
  • 435
  • 5
  • 15
0

You need to add this method

//this function will open your photolibrary

-(void) openGallery {
    UIImagePickerController *imagePicker;

    imagePicker = [[UIImagePickerController alloc] init];



    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;



    imagePicker.delegate = self;

    [self presentViewController:imagePicker animated:YES completion:^{



    }];

}

//you can adjust your image to different modes + library will be dismissed when image is selected

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

    [self dismissViewControllerAnimated:YES completion:^{

    }];
    self.imgView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.imgView.contentMode = UIViewContentModeScaleAspectFit;

}

// to cancel without selecting

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

    [picker dismissViewControllerAnimated:YES completion:NULL];

    }

and finally just call the function in viewWillAppear

[self openGallery];

plus add

[super viewWillAppear:animated];

in your viewWillAppear method

Saad Chaudhry
  • 1,392
  • 23
  • 37
  • i added this code but it still just keeps reopening the imagepicker – C.Wetherell Apr 30 '14 at 14:04
  • @C.Wetherell check the updated code, you can put your own condition to check the photo gallery available etc – Saad Chaudhry Apr 30 '14 at 14:11
  • done all this, im now getting "use of undeclared identifier'openGallery' think ive forgot to do something just cant figure out what *tired* :( thank you so much for your help – C.Wetherell Apr 30 '14 at 14:17
  • man you are doing it wrong way they all are seperate methods wait a sec – Saad Chaudhry Apr 30 '14 at 14:21
  • @C.Wetherell http://pastie.org/9127906 check this replace your code and read the answer care fully again and also the comments in this paste – Saad Chaudhry Apr 30 '14 at 14:25
  • ok, sorry about that i thought it all had to be in my viewDidAppear.. i have changed accordingly now but its still doing the same thing, when i click my button to goto the new viewcontroller, it opens the gallery i select an image and then it dismisses the gallery to the new viewcontroller but soon as that happens it then opens Gallery again - im so sorry for this :( i have updated the code in my questions, I'm not getting errors now thankfully :) – C.Wetherell Apr 30 '14 at 15:01