I have a viewcontroller called via UIStoryboardPopoverSegue and inside there is a button to call the imagePickerController, it's ok for the first call but the second time it crash.
The code is use is ok when it is not through UIStoryboardPopoverSegue.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum (image, nil, nil , nil);
[self dismissViewControllerAnimated:YES completion:nil];
}
-(IBAction)takephoto:(id)sender
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imagePicker.delegate=self;
self.imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
if (!self.imagePicker)
{
self.imagePicker = [[UIImagePickerController alloc] init];
}
}
I enabled NSZobmbie to show any log that can help me tracing it.
* -[UIImagePickerController isKindOfClass:]: message sent to
deallocated instance 0x1eb3b700
Your UIImagePickerController is released and then attempted to be accessed later - so crashes!
To fix it, make the UIImagePickerController a strongly referenced property of the owning instance:
#property (strong, nonatomic) UIImagePickerController *imagePicker;
initialise it once:
if (!self.imagePicker) self.imagePicker = [[UIImagePickerController alloc] init];
and use:
[self.imagePicker doStuff]
for access.
That should solve it.
EDIT
if ([[segue identifier]isEqualToString:#"tcwindshield"]) {
self.ips = [segue destinationViewController];
self.ips.delegate = self
self.ips.strStatValue=#"WindShield";
}
Related
This might be a strange case so I apologize in advance if my problem or solution is unclear:
I have a LoginViewController that fetches a users Facebook profile picture, username, email, etc. I then have it segue to a HomeViewController that displays some objects, other items, and a UIView that shows the users profile picture.
The strange part is that the UIImage that i create only gets transferred when I use a UIButton. I can not seem to get the image to be sent to the HomeViewController any other way. I even set up a GCP to have it try and wait in order for Facebook to deliver the information - still nothing. Below is the code that I have for the LoginViewController.m. If anyone has any idea on why this is happening I would be extremely grateful. thank you!
#import "LoginViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface LoginViewController ()
- (void)toggleHiddenState:(BOOL)shouldHide;
#end
#implementation LoginViewController
#synthesize profPicture;
- (void)viewDidLoad {
[super viewDidLoad];
[self toggleHiddenState:YES];
self.lblLoginStatus.text = #"";
self.loginButton.readPermissions = #[#"public_profile", #"email"];
self.loginButton.delegate = self;
// Do any additional setup after loading the view.
}
-(void)toggleHiddenState:(BOOL)shouldHide{
self.lblUsername.hidden = shouldHide;
self.lblEmail.hidden = shouldHide;
self.profilePicture.hidden = shouldHide;
self.loggedinwallpaper.hidden = shouldHide;
self.FBlogout.hidden = shouldHide;
}
-(void)loginViewShowingLoggedInUser:(FBLoginView *)loginView{
self.lblLoginStatus.text = #"";
[self toggleHiddenState:NO];
}
-(void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView{
self.lblLoginStatus.text = #"";
[self toggleHiddenState:YES];
}
-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user{
NSLog(#"%#", user);
self.profilePicture.profileID = user.objectID;
self.lblUsername.text = user.name;
self.lblEmail.text = [user objectForKey:#"email"];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
// Create Facebook Profile Picture from User ID url
NSString *pic_link = [NSString stringWithFormat:#"http://graph.facebook.com/%#/picture?width=300&height=300", user.objectID];
NSURL *pic_url = [NSURL URLWithString:pic_link];
profPicture = [UIImage imageWithData: [NSData dataWithContentsOfURL:pic_url]];
dispatch_async(dispatch_get_main_queue(), ^(void){
//Main Thread : UI Updates
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *homeViewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"HomeViewController"];
[self performSelector:#selector(prepareForSegue:sender:) withObject:nil afterDelay:1.0 ];
[self presentViewController:homeViewController animated:YES completion:nil];
});
});
}
-(void)loginView:(FBLoginView *)loginView handleError:(NSError *)error{
NSLog(#"%#", [error localizedDescription]);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
HomeViewController *homeviewController = segue.destinationViewController;
homeviewController.homepic = profPicture;
}
#end
From the code you shared I'd assume that the button you mentioned worked because you wired that up in the Storyboard with a segue, right? (Dragged an arrow to the next VC?)
The issue with your code is that here
[self performSelector:#selector(prepareForSegue:sender:) withObject:nil afterDelay:1.0 ];
you are triggering prepareForSegue:sender: on your own without the necessary parameters - you don't have a segue to prepare for as you aren't even segueing to the other view controller. So when HomeViewController *homeviewController = segue.destinationViewController; is executed segue is nil so the image can't be set.
You should be able to fix this by replacing your code which runs on the main thread with this:
dispatch_async(dispatch_get_main_queue(), ^(void){
// Main Thread : UI Updates
[self performSegueWithIdentifier:#"<insert segue identifier here>" sender:self];
});
Make sure to set a name in your Storyboard for the segue between the view controllers and use that identifier here. (See the Apple Docs)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
HomeViewController *homeviewController = segue.destinationViewController;
homeviewController.homepic = [UIImage alloc] init];
homeviewController.homepic = profPicture;
}
Yes, It might be a duplicate question of this. But since it didn't get an answer, I will be more specific on the case and code:
I have 3 involved UIViewControllers:
WelcomeView - the first one
TakePhotoViewController - the second one who is delegate of the OverlayviewController
OverlayViewController - custom view for the camera.
Scenario:
User enter WelcomeView and clicks on a button to be transfered with segue to TakeView.
UIImageViewController is being opened to take a photo.
The user clicks on cancel button - didCancelCamera method in TakePhotoViewController is being invoked and he returns to WelcomeView
The user leaves the app.
The user re-opens the app and perform step 1 again.
THE IMAGE PICKER IS NOT BEING OPENED. I COULD TAKE A PHOTO AND IT'S OK - BUT THE USER CAN'T SEE WHAT HE IS TAKING.
OverlayViewController.h
#interface OverlayViewController : BaseViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
#property (nonatomic,weak) id<OverlayViewControllerDelegate> delegate;
#property (nonatomic,retain) UIImagePickerController *imagePickerController;
#end
OverlayViewController.m:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.imagePickerController = [[UIImagePickerController alloc] init];
self.imagePickerController.delegate = self;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
}
- (IBAction)takePicture:(id)sender {
[self.imagePickerController takePicture];
}
- (IBAction)cancelImagePicker:(id)sender {
[self.delegate didCancelCamera];
}
- (void) setupImagePicker:(UIImagePickerControllerSourceType) sourceType
{
self.imagePickerController.sourceType = sourceType;
if (sourceType == UIImagePickerControllerSourceTypeCamera)
{
self.imagePickerController.showsCameraControls = NO;
if ([[self.imagePickerController.cameraOverlayView subviews] count] ==0)
{
CGRect overlayViewFrame = self.imagePickerController.cameraOverlayView.frame;
CGRect newFrame = CGRectMake(0.0, CGRectGetHeight(overlayViewFrame)-self.view.frame.size.height-10.0, CGRectGetWidth(overlayViewFrame), self.view.frame.size.height + 10.0);
self.view.frame = newFrame;
[self.imagePickerController.cameraOverlayView addSubview:self.view];
}
}
}
- (void)finishAndUpdate
{
[NSThread detachNewThreadSelector:#selector(threadStartAnimating:) toTarget:self withObject:nil];
[self.delegate didFinishWithCamera]; // tell our delegate we are done with the camera
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self finishAndUpdate];
}
TakePhotoViewController.h
#interface TakePhotoViewController : BaseViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,OverlayViewControllerDelegate>
#property (nonatomic, retain) OverlayViewController *overlayViewController;
#end
TakePhotoViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
// Insert the overlay
self.overlayViewController = (OverlayViewController *)[sb instantiateViewControllerWithIdentifier:#"Overlay"];
self.overlayViewController.delegate = self;
}
- (void)viewDidUnload
{
self.overlayViewController = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (void)openImagePicker {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
[self showImagePicker:UIImagePickerControllerSourceTypeCamera];
}
else{
[self showImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
}
}
- (void)viewDidAppear:(BOOL)animated{
if (appDelegate.shouldOpenPicker){
[self openImagePicker];
}
}
- (void)showImagePicker:(UIImagePickerControllerSourceType)sourceType
{
if ([UIImagePickerController isSourceTypeAvailable:sourceType])
{
[self.overlayViewController setupImagePicker:sourceType];
[self presentViewController:self.overlayViewController.imagePickerController animated:YES completion:nil];
}
}
-(void)didCancelCamera{
[[self.overlayViewController.imagePickerController presentingViewController] dismissViewControllerAnimated:NO completion:^ {
[self performSegueWithIdentifier:#"fromTakeToWelcome" sender:self];
}];
}
I found the bug.
The method
-(void)didCancelCamera from TakePhotoViewController is being called when the user clicks on - (IBAction)cancelImagePicker:(id)sender in OverlayViewController.
However, somehow the code in didCancelCamera causes viewDidAppear method of TakePhotoViewController to be invoked again and reopen the image picker.
I have no idea why
[[self.overlayViewController.imagePickerController presentingViewController] dismissViewControllerAnimated:NO completion:^ {
[self performSegueWithIdentifier:#"fromTakeToWelcome" sender:self];
}];
causes the viewDidAppear method of that view (TakePhoto) being recalled again.
Hope that it will help someone
I'm trying to use UIImagePickerController to grab a photo from the users Photos on their iPhone / iPad. This code works just fine for iPhone, but when I run it on iPad, the debugger gives me the message "Terminating app due to uncaught exception 'NSGenericException', reason: '-[UIPopoverController dealloc] reached while popover is still visible.". I'm very new to Objective-C, so I'm unsure of whats causing this, I do not dealloc anything and I have ARC turned on. Here is my code:
ViewController.m
#import "PhotoViewController.h"
#implementation PhotoViewController
#synthesize grabButton;
#synthesize image;
#synthesize imgPicker;
- (IBAction)grabImage {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
[popover presentPopoverFromRect:self.image.bounds inView:self.image permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
[self presentModalViewController:imgPicker animated:YES];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
image.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsImageEditing = YES;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
UIPopover is an ugly patchwork beast of an object. It must be a strong property and an iVar to ensure that Dealloc isn't reached prematurely. Add this in the .h Like so:
#interface MyClass: NSObject {
UIPopover *_popover;
}
#property (nonatomic, strong) UIPopover * popover;
//.m
#synthesize popover = _popover;
When you instantiate the popover, assign it to either the property or the instance:
self.popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
or
_popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
I am using an Alert view to bring up a popup where the user will choose to either select a photo from the library or take a photo to use. The alert view comes up fine, but when I select a button the code I have implemented is not run?!?
for some reason the - (void)picturePopup:(UIAlertView *)picturePopup clickedButtonAtIndex:(NSInteger)buttonIndex does not seem to even get run!? I'm lost run over lots of tutorials and websites but can't see why?! please help!
code:
.m
#import "LoadViewController.h"
#implementation LoadViewController
int imageCase;
- (IBAction)pick:(id) sender {
imageCase = [sender tag];
UIAlertView *picturePopup = [[UIAlertView alloc]
initWithTitle:#"Select Photo" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Choose From Library", #"Take Photo", nil];
[picturePopup show];
}
- (void)picturePopup:(UIAlertView *)picturePopup clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(#"***************getting here****************");
if (buttonIndex == 1) {
NSLog(#"***************library****************");
//Library Picker
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
}
if (buttonIndex == 2) {
NSLog(#"***************camera****************");
//Camera
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
switch (imageCase) {
case 1:
imageView1.image = image;
break;
case 2:
imageView2.image = image;
break;
}
[picker.parentViewController dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker.parentViewController dismissModalViewControllerAnimated:YES];
}
#end
.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface LoadViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIAlertViewDelegate> {
IBOutlet UIImageView *imageView1;
IBOutlet UIImageView *imageView2;
}
- (IBAction)pick:(id) sender;
#end
You need to use the actual delegate method:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
This is not the same as:
- (void)picturePopup:(UIAlertView *)picturePopup clickedButtonAtIndex:(NSInteger)buttonIndex
The method signatures are different. alertView:clickedButtonAtIndex: vs. picturePopup:clickedButtonAtIndex:
You can rename the variable you just can not change the method signature.
This is sort of off topic, but I notice you didn't synthesize *imageView1 and *imageView2. lol
I am using UIimagepickercontroller to browse or take a photo and display it on a subview.
-(IBAction) getPhoto:(id) sender {
secondView1 = [[secondView alloc]
initWithNibName:#"secondView"
bundle:nil];
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if((UIButton *) sender == choosePhotoBtn) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentModalViewController:picker animated:YES];
[self.view addSubview:secondView1.view];
}
This works fine for SourceTypeSavedPhotoAlbum, but if I use camera, the secondview1.view does not show. Instead it only shows the original view.
This is my delegate method:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
secondView1.imageView.image = [info
objectForKey:#"UIImagePickerControllerOriginalImage"];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
[picker release];
}
Thank you for any help!
I finally found out that I should do:
secondView1 = [[secondView alloc]
initWithNibName:#"secondView"
bundle:nil];
self.view addSubview:secondView1.view];
in the delegate function.
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
...
}
Otherwise, the addsubview could be called before the image picker finishes picking an image. Now understand better about how the code flows and the delegate functions.