1

I have built an camera and library app where I am choosing image from my library and showing it to an imageView. But the problem that is happening is after choosing the image is not showing in imageView.

Here is my code.

@interface ViewController ()
{
    BOOL isImageselected;
    UIImageView *myImage;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                          message:@"Device has no camera"
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles: nil];

         [myAlertView show];

    }

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)addPhotoButton:(id)sender {

    [UIView animateWithDuration:0.3 animations:^{
    // animation here
    self.pickerView.frame = CGRectMake(0.0, 443.0, 320.0, 125.0);
    }];
}

- (IBAction)cameraButton:(id)sender {


[UIView animateWithDuration:0.3 animations:^{
    // animation here
    self.pickerView.frame = CGRectMake(0.0, 568.0, 320.0, 125.0);
}];

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picker animated:YES completion:NULL];


}

- (IBAction)uploadImageButton:(id)sender {

    [UIView animateWithDuration:0.3 animations:^{
    // animation here
    self.pickerView.frame = CGRectMake(0.0, 568.0, 320.0, 125.0);
}];


    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];
    myImage.image = chosenImage;
    isImageselected = YES;
    if (isImageselected == YES) {

        myImage = [[UIImageView alloc]initWithFrame:CGRectMake(8.0, 8.0, 95.0, 95.0)];
        self.addPhotoButton.frame = CGRectMake(106.0, 8.0, 95.0, 95.0);
    }

    [picker dismissViewControllerAnimated:YES completion:NULL];

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

    [picker dismissViewControllerAnimated:YES completion:NULL];

}



@end

I think the problem is happening where I am creating the imageView dynamically but I am not getting the point what is wrong.

iPeter
  • 1,330
  • 10
  • 26

3 Answers3

0

Problem is that you assigning image to UIImageView object myImage before creating myImage.

Try to place the line

myImage.image = chosenImage;

After this line:

    myImage = [[UIImageView alloc]initWithFrame:CGRectMake(8.0, 8.0, 95.0, 95.0)];
Vladimir K
  • 1,382
  • 1
  • 12
  • 27
  • Not happening. I have tried allocating the imageView in viewDidLoad also but I think the imageView is not creating. @Vladimir – iPeter Apr 10 '15 at 07:52
  • Have you added your imageView to view? [self.view addSubview:myImage]; – Vladimir K Apr 10 '15 at 07:56
  • Oops!!!! That was the main problem. @Vladimir I have to learn hard. :( I am such a newbie. :( – iPeter Apr 10 '15 at 07:59
  • 1
    Don't worry about it :) We've all been there. – Vladimir K Apr 10 '15 at 08:01
  • can you please help me in more thing? If I want to choose more than one image from library and place it one after another in my view how can I do that? According to my code if I choose images it is overwriting the previous one. – iPeter Apr 10 '15 at 09:06
  • You can’t select several pictures with UIImagePickerController - only one picture per time. But there are some third party libraries, that allows to do that. Check this answer here: http://stackoverflow.com/a/21280134/4495995 – Vladimir K Apr 10 '15 at 10:02
0

You cant not assign object before creating it, so just change your code a little bit like

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


    isImageselected = YES;
    if (isImageselected == YES) {

        myImage = [[UIImageView alloc]initWithFrame:CGRectMake(8.0, 8.0, 95.0, 95.0)];
        [self.view addSubView:myImage];
        self.addPhotoButton.frame = CGRectMake(106.0, 8.0, 95.0, 95.0);
        UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
        myImage.image = chosenImage; 
   }

    [picker dismissViewControllerAnimated:YES completion:NULL];

}
Retro
  • 3,985
  • 2
  • 17
  • 41
  • No dude! I have understood where I was wrong. I have rectified it but, it's not happening. the imageView is not creating. @Retro – iPeter Apr 10 '15 at 07:56
  • Yeah, as you have created the imageView you have to add it as subView of self.view – Retro Apr 10 '15 at 07:58
0

This because after adding the selected photo to myImage you are re allocating myImage, this create new object of myImage. Try this

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
isImageselected = YES;
if (isImageselected == YES) {

    myImage = [[UIImageView alloc]initWithFrame:CGRectMake(8.0, 8.0, 95.0, 95.0)];
....
myImage.image = chosenImage; }
Chlebta
  • 3,090
  • 15
  • 50
  • 99