因此,我在使用此应用程序时遇到了一些不同的问题,这是我的第一个应用程序,以前从未编写过代码,首先是我无法打开我的按钮来打开“ secondviewcontroller”来显示我的图像,认为我现在已经解决了,现在我的按钮加载了我的“ secondviewcontroller”,它打开了我选择图像的相机胶卷,然后关闭并重新打开了相机胶卷以重新选择另一个图像。

这是我的FirstViewController.h-名为BESPOKEViewController.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


这是我的SecondViewController.h-它称为secondpage.h

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


第二页

#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];
}


更新为所有代码,而不是不是从情节提要中获取源代码,
先感谢您

最佳答案

因为您是从[self openGallery];调用viewDidAppear方法的,所以每次视图出现时都会调用openGallery

发生了什么事:

SecondPage加载然后出现->显示的GalleryView->选择的图像->画廊视图消失->出现SecondPage

当您关闭画廊视图时,viewDidAppear再次在SecondPage上调用,这就是为什么会出现永无止境的循环的原因。

这是viewController生命周期的一部分。因为一次只显示一个viewController,所以即使SecondPage是呈现选择器的视图,它也会再次调用viewDidAppear

因此,您需要做的是在ViewController首次加载时(而不是每次显示时)调用openGallery方法。

您可以尝试将[self openGallery]调用置于viewDidLoad中,或者具有一些逻辑来确定是否要在viewDidAppear中显示图库。

这个问题对于理解viewController的生命周期也有一个很好的答案:Looking to understand the iOS UIViewController lifecycle

10-04 18:18