I am attempting to pass a variable between to view controllers and then show the new view
I have implemented the following code to pass a variable (without using segue)
viewCameraViewController *viewCamera = [[viewCameraViewController alloc] initWithNibName:#"viewCameraViewController" bundle:nil];
viewCamera.str1 = self.str;
[self.navigationController pushViewController:viewCamera animated:YES];
and then this to show the view
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"cameraView"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
however when the new view loads the variable is null
here is my viewCamera class
//
// viewCameraViewController.m
// WebView
//
// Created by Admin on 31/10/2015.
// Copyright (c) 2015 Admin. All rights reserved.
//
#import "viewCameraViewController.h"
#interface viewCameraViewController ()
#end
#implementation viewCameraViewController
- (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)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)takePhoto:(UIButton *)sender {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Device has no camera"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[myAlertView show];
} else {
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController: picker animated:YES completion:NULL];
}
}
-(IBAction)selectPhoto:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController: picker animated:YES completion:NULL];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
#end
im assuming that this is caused by initialising two instances of the class, one called viewCamera and the other called VC.
can anyone help identify where i am going wrong here and point me in the right direction.
Thanks in advance
If you have a View Controller that is defined in Interface Builder, you don't want to instantiate it with alloc/init, but rather with the method used in your second snippet. Make sure that you have set the class of your view controller in interface builder, and then cast it to your class to set the variable.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
YourViewControllerClass *vc = (YourViewControllerClass *)[storyboard instantiateViewControllerWithIdentifier:#"cameraView"];
vc.yourStringVariable = self.str;
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
Note that presentViewController and pushViewController are two different ways of displaying a view controller. The first one presents it modally, the other one pushes it to a navigation controller. You don't want both of these calls.
Update after your update: so, YourViewControllerClass is viewCameraViewController - however, I'd point out that's an oddly named class; class names should start with upper case. Name it CameraViewController instead, is my suggestion. And don't use initWithNib: if you have your view controller defined in a storyboard, just remove that.
Related
NSZombie detected that one of the objected is over released in my app and that is causing the app to crash every time when a button is pressed. However, after inspecting the source code of where the over release happens, I couldn't see any obvious code that that may have caused the release. Can xcode release objects automatically without any actual code?
Below are the places reported by the Instrument that has a release event:
-(void) takePicture:(CDVInvokedUrlCommand *)command {
CDVCameraPicker* cameraPicker = [[CDVCameraPicker alloc] init];
self.pickerController = cameraPicker;
CameraOverlayViewController* overlay = [[CameraOverlayViewController alloc] initWithNibName:#"CameraOverlayViewController" bundle:nil];
cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraPicker.cameraOverlayView = overlay.view;
[self.viewController presentViewController:cameraPicker animated:YES completion:nil];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
Any ideas on how to fix the problem? Thanks in advance!
I think CameraOverlayViewController is released.
Try the below code
CameraOverlayViewController* overlay;
-(void) takePicture:(CDVInvokedUrlCommand *)command {
CDVCameraPicker* cameraPicker = [[CDVCameraPicker alloc] init];
self.pickerController = cameraPicker;
overlay = [[CameraOverlayViewController alloc] initWithNibName:#"CameraOverlayViewController" bundle:nil];
cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraPicker.cameraOverlayView = overlay.view;
[self.viewController presentViewController:cameraPicker animated:YES completion:nil];
}
You're creating a CameraOverlayViewController and keeping a reference to it's view, but the CameraOverlayViewController itself is going out of scope when the takePicture: method ends. A view doesn't retain it's view controller. You need to either keep a reference to the CameraOverlayViewController, or if you're only interested in the view object itself, just create a view from the xib file and set the cameraPicker.cameraOverlayView to that view.
EDIT:
For example, in your header file you could make a property:
#property (strong) CameraOverlayViewController *overlay;
and in your method:
-(void) takePicture:(CDVInvokedUrlCommand *)command {
CDVCameraPicker* cameraPicker = [[CDVCameraPicker alloc] init];
self.pickerController = cameraPicker;
self.overlay = [[CameraOverlayViewController alloc] initWithNibName:#"CameraOverlayViewController" bundle:nil];
cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraPicker.cameraOverlayView = self.overlay.view;
[self.viewController presentViewController:cameraPicker animated:YES completion:nil];
}
Once you're done with the camera, you'll want to let go of the reference. So for example:
-(void) cameraPickerFinished {
self.overlay = nil;
}
Im trying to set up an imagePicker in a new iOS7 Xcode project using storyboards but can't seem to find any examples online until I found the following code.
I've set it up so that when a button is pressed it uses a modal push thing to navigate to the view which has the class of "UIImagePickerController" which apparently calls in the image picker code.
However when the app is run it won't load anything up after clicking on allow the application to access your photos any chance of some help please?
Error screen shot grab: https://pbs.twimg.com/media/Bdy7v9JCAAABpvS.jpg:large
interface setup:
#interface ridecount_AddRide_ViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
code:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
UIImagePickerController *controller = [segue destinationViewController];
controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
controller.delegate = self;
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[[self.view viewWithTag:100023] removeFromSuperview];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(320/2-200/2, 10, 100, 100)];
imageView.tag = 100023;
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = image;
[self.view addSubview:imageView];
}
Initialize the controller UIImagePickerController *controller = [[segue destinationViewController] init];
for SWIFT - bit different approach
//Init property
var imagePicker = UIImagePickerController()
then assign
override func prepareForSegue(segue:UIStoryboardSegue!, sender: AnyObject!)
{
imagePicker = segue.destinationViewController as UIImagePickerController
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.allowsEditing = false
imagePicker.delegate = self
}
I had the same problem. The UIImagePickerController object which was initialized from the storyboard worked correctly only when the availableMediaTypesForSourceType was set to UIImagePickerControllerSourceTypeCamera.
If you want to preserve prepareForSegue: functionality you can remove the UIImagePickerController from the storyboard and create it programatically using the following code:
- (void)showCameraRollController
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
UIStoryboardSegue *segue = [UIStoryboardSegue segueWithIdentifier:#"CameraRollController" source:self destination:imagePickerController performHandler:^{
[self presentViewController:imagePickerController animated:YES completion:NULL];
}];
[self prepareForSegue:segue sender:self];
[segue perform];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"CameraRollController"]) {
UIImagePickerController *imagePickerController = [segue destinationViewController];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
}
}
Possibly, another alternative is to subclass UIImagePickerController and add to the subclass:
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
return [super init];
}
It seemed to work.
Something strange is happening with my code.
Scenario: Open camera->Take picture->Pass the image taken to a viewcontroller(from a storyboard.
The thing is that my UIimage variable from the destination view is not available, not recognized!
My code
postviewController.h
#import <UIKit/UIKit.h>
#interface postAlertViewController : UIViewController
#property (nonatomic, retain) UIImage *cameraImage;
#end
postviewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *aImageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width,150 )];
aImageview.image=cameraImage;
[self.view addSubview:amageview];
}
Take picture and pass it to the view controller above
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:NULL];
ViewController *postView = [self.storyboard instantiateViewControllerWithIdentifier:#"postview"];
postView.cameraImage=(UIImage *)[info objectForKey:UIImagePickerControllerEditedImage];
[self.navigationController pushViewController:postView animated:YES];
}
The stroryboard ID is set to "postview".
What i am getting is:
Property 'cameraImage' not found on object of type 'ViewController *'
Why cameraImage is not available from the origin view although is declared in destination's .h file?
In another situation where i needed to pass a string between views, in same manner as above
[yourViewController setValue:mysc.title forKey:#"sendAlertTitle"];
worked ok.
Any help?
Thank you.
try this
postAlertViewController *postView = [self.storyboard instantiateViewControllerWithIdentifier:#"postview"];
and always use class and interface name start with capital
you can use this one for more ease.
- (IBAction)captureImage:(id)sender
{
if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *deviceNotFoundAlert = [[UIAlertView alloc] initWithTitle:#"No Device" message:#"Camera is not available"
delegate:nil
cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[deviceNotFoundAlert show];
} else {
UIImagePickerController *cameraPicker = [[UIImagePickerController alloc] init];
cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraPicker.delegate =self;
// Show image picker
[self presentViewController:cameraPicker animated:YES completion:nil];
}
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
UIImage *selectedImage =[info objectForKey:UIImagePickerControllerOriginalImage];
ViewControllerName *Vc=[self.storyboard instantiateViewControllerWithIdentifier:#"captioEdit"];
Vc.postImgStr=[self scaleAndRotateImage:selectedImage];
[picker dismissViewControllerAnimated:YES completion:nil];
[self.navigationController pushViewController:captioViewEdit animated:YES];
}
//Delegate method of UIImagePickerController for image picker model cancel
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
Thanks
I have two views. Very simple set up just to play around with the camera roll.
One view will bring up the camera roll using:
[self presentViewController:self.imagePicker animated:NO completion:nil];
which is triggered by a UIButton. It brings up the camera, everything's fine.
After taking a picture, I am calling the following to dismiss the controller:
[self dismissViewControllerAnimated:NO completion:nil];
Again, everything is working.
But the view that brings up the imagePicker Controller no longer receives touch (the UIButton will no longer brings up the camera again) after the Modal View is dismissed.
It will only start to receive touches again when I switch to another view and come back.
I have been searching for a solution to this problem but have not been successful in finding anything. Thanks in advance.
EDIT (Adding code):
In CameraController.m
This is where brings up the Camera Roll
(Subclass of UIViewController conforming to
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>)
//UIButton that brings up the imagePicker
- (IBAction)useCamera
{
[self prepareImagePicker];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
//Initializing ImagePicker
- (void)prepareImagePicker
{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage, nil];
self.imagePicker.delegate = self;
OverlayCameraView *cameraOverlay = [[OverlayCameraView alloc] initWithFrame:(CGRect){{0, 0}, 320, 480} andTheCameraController:self];
self.imagePicker.cameraOverlayView = cameraOverlay;
}
}
//Delegate Method when a picture is taken
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
self.imagePicker.delegate = nil;
[picker dismissViewControllerAnimated:NO completion:nil];
self.imagePicker = nil;
self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
In OverlayCameraView.m
//Camera Overlay Class (Subclass of UIView)
- (id)initWithFrame:(CGRect)frame andTheCameraController:(CameraController*)cameraController
{
if ((self = [super initWithFrame:frame]))
{
self.camera = cameraController;
//.…2 UIButtons set up
}
return self;
}
//Overlay Cancel Button
- (void) cancel
{
[self.camera dismissViewControllerAnimated:NO completion:nil];
}
//Overlay Take Picture Button
- (void) takeAPicture
{
[self.camera.imagePicker takePicture];
}
You should be calling dismissViewControllerAnimated on the picker not self via the UIImagePickerControllerDelegate methods.
[picker dismissViewControllerAnimated:NO completion:nil];
or if you are keeping a reference to it:
[self.imagePicker dismissViewControllerAnimated:NO completion:nil];
EDIT:
Based on the revised code, I believe
self.imagePicker.cameraOverlayView = cameraOverlay;
should be:
self.imagePicker.cameraOverlayView = cameraOverlay.view;
Also this will cause a +2 reference count and will leak if you are not using ARC:
self.imagePicker = [[UIImagePickerController alloc] init];
It should be for proper reference counting:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
self.imagePicker = picker;
[picker release];
In my application i have used the following code to implement after dismissing the view i have pushed to new view,When i tried to implement the view is not dismissing instead it overlapping.Here my code,
-(IBAction)selectExitingPicture
{
if([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker= [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
[picker dismissViewControllerAnimated:YES completion:^{
cropingImageViewCon = [[CropingImageViewControl alloc]initWithNibName:#"CropingImageView" bundle:nil];
cropingImageViewCon.delegate = self;
cropingImageViewCon.originalImg = image;
[self.navigationController presentModalViewController:cropingImageViewCon animated:YES];
}];
}
Whats wrong with my code,Can any one please help out.
If you want to push into CropingImageViewControl then you are going wrong way,
Use
[self.navigationController pushViewController:yourViewCotrollerObject animated:YES];
instead of using presentModalViewController: