Pergunta

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

Foi útil?

Solução

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

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top