选择图像后,相机胶卷会一直显示

时间:2014-04-30 13:49:23

标签: ios uiimagepickercontroller

所以我对这个应用程序有一些不同的问题,它是我的第一个应用程序,从未编码过,首先是我无法打开一个“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

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

更新到所有代码,而不是来自storyboard,会 提前谢谢

2 个答案:

答案 0 :(得分:2)

由于您从[self openGallery];调用了viewDidAppear方法,因此每次出现视图时都会调用openGallery

发生的事情是:

第二页加载然后出现 - &gt; GalleryView显示 - &gt;选择的图像 - &gt;图库视图消失 - &gt;第二页出现

当你关闭图库视图时,会再次在SecondPage上调用viewDidAppear,这就是为什么你会得到一个永无止境的循环。

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

所以你需要做的是在第一次加载ViewController时调用openGallery方法,而不是每次都显示它。

您可以尝试将[self openGallery]电话放入viewDidLoad,或者有一些逻辑来确定您是否希望图库显示在viewDidAppear中。

这个问题也很好地理解了viewController生命周期:Looking to understand the iOS UIViewController lifecycle

答案 1 :(得分:0)

您需要添加此方法

//此功能将打开您的照片库

-(void) openGallery {
    UIImagePickerController *imagePicker;

    imagePicker = [[UIImagePickerController alloc] init];



    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;



    imagePicker.delegate = self;

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



    }];

}

//您可以将图像调整为不同模式+选择图像时图书馆将被取消

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

    [self dismissViewControllerAnimated:YES completion:^{

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

}

//取消而不选择

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

    [picker dismissViewControllerAnimated:YES completion:NULL];

    }

最后只需调用viewWillAppear

中的函数
[self openGallery];

加上

[super viewWillAppear:animated];
您的viewWillAppear方法

中的