I'm trying to handle a case where if an image is successfully loaded from the photo library, then DO trigger push segue to another view controller, which is where the image will be displayed in an image view. If no image is loaded, say the cancel button is pressed, then DON'T trigger push segue, and stay on the current (root) view.
This is an app for iPhone.
What I have currently does not work, and sometimes I get this warning when I cycle through the process of accessing the photo library and selecting an image / canceling:
Warning: Attempt to present < selectDisplayViewController: 0x11d45360 > on < UINavigationController: 0x8f58ee0 > while a presentation is in progress!
My code is as follows:
ViewController.m
#import "ViewController.h"
#import "selectDisplayViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)LibraryButton:(id)sender
{
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePicker animated:YES completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
/*UIImage *loadImage;
loadImage = [info valueForKey:UIImagePickerControllerOriginalImage];
buffer = loadImage;*/
modBuffer = nil;
modBuffer = [info valueForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
CGImageRef cgImRef = [modBuffer CGImage];
CIImage *cIm = [modBuffer CIImage];
if(cIm != nil && cgImRef != NULL)
{
return true;
buffer = modBuffer;
}
return false;
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
selectDisplayViewController *secondVC = [[selectDisplayViewController alloc]init];
[self presentViewController:secondVC animated:YES completion:nil];
}
- (void)viewDidLoad
{
imagePicker = [[UIImagePickerController alloc]init];
[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.
}
@end
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
UIImagePickerController *imagePicker;
UIImage *modBuffer;
}
- (IBAction)LibraryButton:(id)sender;
@end
selectDisplayViewController.m
#import "selectDisplayViewController.h"
@interface selectDisplayViewController ()
@end
@implementation selectDisplayViewController
@synthesize selectDisplayView;
- (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.
selectDisplayView.image = buffer;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
selectDisplayViewController.h
#import <UIKit/UIKit.h>
@interface selectDisplayViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *selectDisplayView;
@end
UIImage *buffer;
All I can get it to do is load the image. It doesn't go to the display view controller.
I greatly appreciate any help or suggestions.