Display photos in different UIImageViews - ios

In my test app, I have two buttons (takePhoto1 and takePhoto2) and two UIImageViews (photo1View and photo2View). The first button takes a photo and displays it in the first image view. The second button takes another photo, and is supposed to display it in the second image view. However, for some reason second photo is displayed in photoView1 instead of photoView2.
I haven't implemented any code to save the photos yet, so I'm not worried about the photos persisting in the image views.
What I do need to figure out is how to make sure when the second photo is taken, it is displayed in the appropriate view. Here is what I have so far:
//Take photo1 and display in top image view
- (void)takePhoto1;
{
[self dismissViewControllerAnimated:YES completion:NULL];
if ([self.capturedImages count] > 0)
{
if ([self.capturedImages count] == 1)
{
// Camera took a single picture.
[self.photo1View setImage:[self.capturedImages objectAtIndex:0]];
}
// To be ready to start again, clear the captured images array.
[self.capturedImages removeAllObjects];
}
self.imagePickerController = nil;
}
//Take photo2 and display in bottom image view
- (void)takePhoto2;
{
[self dismissViewControllerAnimated:YES completion:NULL];
if ([self.capturedImages count] > 0)
{
if ([self.capturedImages count] == 1)
{
// Camera took a single picture.
[self.photo2View setImage:[self.capturedImages objectAtIndex:1]];
}
// To be ready to start again, clear the captured images array.
[self.capturedImages removeAllObjects];
}
self.imagePickerController = nil;
}
Anyone know how to fix this?

I saw your questions an couldn't help my self to solve it in my way. If I had same problem as yours I would have solved it in this way:
Consider, button's tag is same as, imageView tag.
#import "ViewController.h"
#interface ViewController ()
- (IBAction)takePhoto:(id)sender;
#property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *imageViews;
#property int tag;
#end
#implementation ViewController
- (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)takePhoto:(id)sender {
_tag = ((UIButton *)sender).tag;
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
#pragma mark - UIImagePickerDelegate Methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
[picker dismissViewControllerAnimated:NO completion:nil];
[[self getImageView:_tag] setImage:image];
}
- (UIImageView *) getImageView : (int) tag{
for (UIImageView *imageView in _imageViews) {
if (imageView.tag == tag) {
return imageView;
}
}
return nil;
}
#end
Here you can check this as working project here.

Check your storyboard or xib to see if "photo2View" is really connected to your outlet.
I suspect the UIImageView for "photo1View" is also connected to "photo2View".
p.s. you should also change this line in your #implementation:
- (void)takePhoto2;
to this:
- (void)takePhoto2
semi-colons should only come after method declarations in your .h file.

Does your code work? I find two issues -
Issue 1 - This should crash as capturedImages has one element according to second if check but you are accessing second element in the array while setting the imageView.
if ([self.capturedImages count] == 1)
{
// Camera took a single picture.
[self.photo2View setImage:[self.capturedImages objectAtIndex:1]];
Issue 2 - Since you are clearing the self.capturedImages, this should have thrown you can exception in second method.
[self.photo2View setImage:[self.capturedImages objectAtIndex:1]];

Did you properly connect button 1 to takePhoto1 and button 2 to takePhoto2? Maybe button 2 is also doing takePhoto1?

Related

Popover presentation on an iPhone using UIPopoverPresentationController

Hello everyone I want to make a popover for iPhone so here is my code where I am geting wrong please suggest I want to create somthing like below pic but mine is covering the whole screen and I dont want to use segue .So basicaly I want if I click on button a resized View controller should popup how can I achieve this please help.I had followed this tutorial.
https://www.youtube.com/watch?v=UQBbJQNEDA4
#import "ViewController.h"
#import "Popup.h"
#interface ViewController ()<UIPopoverPresentationControllerDelegate>
{
Popup * popupController;
UIPopoverPresentationController *popupPresentationController;
}
#end
#implementation ViewController
- (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)popupOnClick:(id)sender {
popupController=[self.storyboard instantiateViewControllerWithIdentifier:#"Popup"];
popupController.modalPresentationStyle=UIModalPresentationPopover;
popupPresentationController= [popupController popoverPresentationController];
popupPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
popupController.preferredContentSize=CGSizeMake(150, 300);
popupPresentationController.delegate = self;
[self presentViewController:popupController animated:YES completion:NULL];
// in case we don't have a bar button as reference
popupPresentationController.sourceView = _popupButton;
popupPresentationController.sourceRect = _popupButton.frame;
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
return UIModalPresentationNone;
}
#end
Here's your problem:
[self presentViewController:popupController animated:YES completion:nil];
popupPresentationController= [popupController popoverPresentationController];
popupPresentationController.delegate = self;
That code is in the wrong order. You must set the delegate before calling presentViewController.

IBAction cycle through images on tap

I want to use an IBAction method that when on tap cycles through images. Ie one image and a button is shown unload and when the user taps the button a new image is shown in its place etc. Right now it just shows the first image in the array. When I tap the button a second time nothing happens. Can someone please help me edit this to cycle through all of my images? Thanks.
EDIT
//when i tap the button only the first image shows in the array. What should I edit from the current app?
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (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.
}
//Implementing our method
- (IBAction)buttonPressed
{
NSArray *imagesArray = #[#"image1.png",#"image2.png",#"image3.png",#"image4.png"];
count ++;
if(count == imagesArray.count)
{
count = 0;
}
yourImageView.image = [UIImage imageNamed:[imagesArray objectAtIndex:count]];
}
#end
try this....
add images into your project and store those images names into an NSArray
for ex.
//declare imagesArray as instance variable
NSArray *imagesArray = #[#"image1.png",#"image2.png",#"image3.png",#"image4.png"];
and in the button action method
- (IBAction)buttonPressed
{
count ++;
if(count == imagesArray.count)
{
count = 0;
}
yourImageView.image = [UIImage imageNamed:[imagesArray objectAtIndex:count]];
}

Extend the UIImageView to be a swipeable element that can display multiple photos

i have created a simple app with two buttons and one uiimageview.
One button takes a photo from photo library and puts it on uiimageview.
When the user adds another photo the old one should be saved and then user can swipe between these two photos.
another button takes a photo and put it on uiimageview.
So now i'm really confused about swiping between photos. I read that uiimageview can contain only one image. Also, i read that i need to add uiScrollView to UiImageView, but i don't know how they work together.
should i delete my UIImageView and then create ScrollView instead of it?
Here is my code
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)pickPhoto:(id)sender {
picker1 = [[UIImagePickerController alloc]init];
picker1.delegate = self;
[picker1 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker1 animated:YES completion:NULL];
}
- (IBAction)takePhoto:(id)sender {
picker2 = [[UIImagePickerController alloc]init];
picker2.delegate = self;
if([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){
}
else{
NSLog(#"not avaialbe");
}
[self presentViewController:picker2 animated:YES completion:NULL];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self.imageView setImage:image];
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
UPDATE
So i have added a method that allows user to swipe left or right and he can see different pictures, however, i had to hard code my images into the code. But i want to select images from the photo library, then these images will be saved in the array and then if user adds more pictures he has a choice to swipe through his old images.
Here is my code for swiping through the images
- (IBAction)Swipe:(id)sender {
NSArray *images = [[NSArray alloc]initWithObjects:#"ayat.png", #"qwe.png",#"4444", #"15", nil];
UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];
switch(direction)
{
case UISwipeGestureRecognizerDirectionLeft:
imageIndex++;
break;
case UISwipeGestureRecognizerDirectionRight:
imageIndex--;
break;
default:
break;
}
imageIndex = (imageIndex <0)?([images count] -1 ):
imageIndex % [images count];
self.imageView.image = [UIImage imageNamed:[images objectAtIndex:imageIndex]];
}
but here i added pictures to my project.
It's true that a UIImageView can display only a single image.
There are various ways to handle this. One would be to use a UIPageViewController. That would enable you to set up side-swipe based paging between pages of content. There is a sample app from Apple called PhotoScroller (it used to be linked from the Xcode help system. With Xcode 6, apple seems to have removed all sample code. You'll have to search the net for the sample app. That app also includes tiled rendering, which is likely overkill for your application, but it does show how to set up a page view controller.
You could also use a UICollectionView, or create your own custom view that manages a set of views.
Actually now that I think about it a UICollectionView set up for paging might be your best bet.

iOS app crashes without any exception after tapping IBOutlet with assigned IBAction (UIButton)

My task is to create custom UIAlertView with multiline text input field and everything works pretty much well up to the point where I need to do some actions if dismiss button was tapped. As example I'm using: http://iphonedevelopment.blogspot.co.uk/2010/05/custom-alert-views.html
I have created very simple popup for testing purpose. Basically it's UIViewController xib with single button in it. The main class is demonstrated below:
#import "testViewController.h"
#interface testViewController ()
#end
#implementation testViewController
- (id)init
{
self = [super initWithNibName:#"testViewController" bundle:nil];
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)showInView:(UIView *)view
{
[view addSubview:[self view]];
[[self view] setFrame: [view bounds]];
[[self view] setCenter:[view center]];
}
- (IBAction)onTap:(id)sender {
NSLog(#"All OK");
}
#end
then in root UIViewController I'm calling my custom alert:
- (IBAction)showAlert:(id)sender
{
dispatch_async(dispatch_get_main_queue(), ^{
testViewController *alert = [[testViewController alloc] init];
[alert showInView:[self view]];
});
}
So far I have tried Main thread or global queue or even synchronous dispatch, but everything ends up with break on:
int main(int argc, char * argv[]) {
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); <-- Thread 1:EXC_BAD_ACCESS (code=1, address=0xa000000c)
}
}
tried to add observer for the button but still no go..
Any help is much appreciated
Here what happing is "if you want to add one viewcontroller view to other viewcontroller's view then after adding of views you should convey to the compiler that second view controller is going to be child to the first"
i.e pre intimation of parent to child relationship has to be done.
now just modify your showAlert method with the following . it is working 100%.
- (IBAction)showAlert:(id)sender {
dispatch_async(dispatch_get_main_queue(), ^{
testViewController *alert = [[testViewController alloc] init];
[alert showInView:[self view]];
[self addChildViewController:alert];
[alert didMoveToParentViewController:self];
});
}
Check if your UIButton is assigned multiple IBoutlets or multiple IBActions. This a common issue which happens without our notice sometimes.
It turns out that my testViewController was released by ARC before I tap the button

How could I use a single UIButton to get a library photo with an image picker and perform a push segue?

I am trying to pick an image from the photo library and perform a segue only if the user selects an image. (Does nothing if the user presses cancel)
Is it possible to pack all of this action into one single button press via IBAction?
The issue I am facing with my attempted code is that it tries to perform the push segue right when I press the button, which apparently is at the same time I am trying to access the photo library. I can't figure out how to get the image picker to go first and then have the push segue trigger immediately after, depending on whether an image was picked or the process was canceled.
ViewController.m
- (IBAction)LibraryButton:(id)sender
{
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePicker animated:YES completion:nil];
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"takeLibraryPhoto"])
{
buffer = modBuffer;
selectDisplayViewController *secondVC = [segue destinationViewController];
}
else if ([segue.identifier isEqualToString:#"takeCameraPhoto"])
{
}
else if ([segue.identifier isEqualToString:#"takeCameraVideo"])
{
}
}
- (BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
if([identifier isEqualToString:#"takeLibraryPhoto"])
{
//[self presentViewController:imagePicker animated:YES completion:nil];
if(modBuffer != NULL)
{
return true;
}
return false;
}
else
{
return true;
}
return false;
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
/*UIImage *loadImage;
loadImage = [info valueForKey:UIImagePickerControllerOriginalImage];
buffer = loadImage;*/
modBuffer = [info valueForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated: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.
}
selectDisplayViewController.m
#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.
}
What my code currently does is... When I load an image, "buffer" of selectDisplayViewController is successfully getting data from "modBuffer", but by then the push segue has already occurred and it returns me to the original view controller that I press the button from. It isn't until I press the button a second time (could also press cancel this time) that it takes me to the other view controller where it displays my image.
I have been struggling with this for many days now. How can I get this to behave more like a sequence where its a step process instead of the push segue happening right away!! :(
All help is greatly appreciated. Thanks :)
Not really sure I completely follow but it sounds like you want to perform the segue after the user selects a picture or presses cancel from the image picker.
If that's the case then you can do that in the delegate methods using [self performSegueWithIdentifier:#"foo" sender:nil];

Resources