imageView doesn't show image from UIImagePickerController - ios

I am trying to programmatically use an image from UIImagePickerController in one view controller and have that be the imageView in another view controller. Here is the method from viewController A
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *image = info[UIImagePickerControllerOriginalImage];
bandCropViewController *viewController = [[bandCropViewController alloc]init];
viewController.imageOriginal = image;
[self dismissViewControllerAnimated:YES completion:^{
// Move to Crop View Controller
[self presentViewController:viewController animated:YES completion:nil];
}];
}
ViewController B looks like this:
- (void)viewDidLoad
{
[super viewDidLoad];
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100, 320, 280)];
[self.imageView setImage:imageOriginal];
imageView.backgroundColor = [UIColor redColor];
[self.view addSubview:imageView];
}
viewController B is showing the imageView frame in red as it should be. But the image is not carrying over.
Any thoughts as to what I am doing wrong?

Highly recommend this tutorial
It should help you out. It looks like you are possibly doing some unnecessary things in
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

Related

How to overlay camera capture image?

I am replacing the part of image by capture image from camera. i need to perform the activity like this app in my application. https://itunes.apple.com/in/app/replace-my-face-funny-girls/id794033892?mt=8
I need a way of how can i take picture from background view with super view image and save the new generated image with merging of both image.
Please suggest me solution.
The simple work i did
- (void)viewDidLoad {
[super viewDidLoad];
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.delegate = self;
self.picker.allowsEditing = NO;
self.picker.showsCameraControls = NO;
self.picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
CGRect overlayRect = CGRectMake(0, 0, self.imageView.frame.size.width,self.imageView.frame.size.height);
UIView *overlayView = [[UIView alloc] initWithFrame:overlayRect];
UIImageView *capture_image=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.imageView.frame.size.width,self.imageView.frame.size.height)];
capture_image.image=[UIImage imageNamed:#"1.jpg"];
[overlayView addSubview:capture_image];
[self.picker setCameraOverlayView:overlayView];
[self.navigationController presentViewController:self.picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Please also suggest me if there is any repository working like this approach.
About capturing the image: you might want to look at FastttCamera, which allows you to set the camera view to your defined image view and crops the image accordingly.
Good luck!

Passing image between view controllers [duplicate]

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 7 years ago.
I read much posts about this question but I don't found any answers..
so, I want to pass from my "ViewController" to my "SecondViewController" an image picked from ImagePicker, this is what I tried:
In my ViewController:
-(IBAction)Scatta:(id)sender {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
}
// image picker needs a delegate
[imagePickerController setDelegate:self];
// Place image picker on the screen
[self presentViewController:imagePickerController animated:YES completion:nil];
// Create main view controller
Main_VCViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:#"Main"];
// Go to main
[self presentModalViewController:VC animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:^{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
Main_VCViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:#"Main"];
VC.ImmaginePresa = image;
[self presentModalViewController:VC animated:YES];
}];
}
and this is my SecondViewController :
-(void)viewDidAppear:(BOOL)animated {
UIImage *imm = _ImmaginePresa;
[_Image setImage: imm];
}
I set a default image in my SecondVC and when I launch the app I press the "Scatta" button and when it opens my SecondVC it shows me for 0.5/1 second my default image, and after that, it shows nothing.
I tried to pass in the same way some strings and it works correctly.
EDIT1
ViewController:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
//Instanzio il viewController della main
Main_VCViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:#"Main"];
NSData *imageData = UIImagePNGRepresentation(img.images);
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:imageData] forKey:#"ImageData"];
VC.ImmaginePresa = img;
[self dismissModalViewControllerAnimated:YES];
//Vado alla main
[self presentModalViewController:VC animated:YES];
}
SecondVC:
-(void)viewDidAppear:(BOOL)animated {
NSData* myEncodedImageData =[[NSUserDefaults standardUserDefaults]objectForKey:#"ImageData"];
UIImage* image = [UIImage imageWithData:myEncodedImageData];
[_Image setImage: image];
}
It still doesn't work, same result
EDIT2
With #SaurabhPrajapati 's solution it seems like the image is nil, because when i click on my image it doesn't do anything is it possible?
I assume that you are getting image in "didFinishPickingMediaWithInfo". so here is my suggestion
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
if(image != nil)
{
[self dismissViewControllerAnimated:YES completion:^{
//define ImmaginePresa as NSData in Main_VCViewController
Main_VCViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:#"Main"];
VC.ImmaginePresa = UIImagePNGRepresentation(image);
[self presentModalViewController:VC animated:YES];
}];
}
}
and in Main_VCViewController you can get image like ,
UIImage *img = [UIImage imageWithData:self.ImmaginePresa];

UIIMagePickerController dismissViewControllerAnimated:completion: not working

I'm trying to pick an image from UIImagePickerController and then set it inline inside UITextView. The code below is pretty straightforward. When i tap "act" button, it opens up UIImagePickerController and once it's done, the delegate method didFinishPickingMediaWithInfo gets triggered.
- (IBAction)act:(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];
if(!self.imageView){
self.imageView = [[UIImageView alloc] initWithImage:chosenImage];
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
[self.imageView setFrame:CGRectMake(10.0, 10.0, 100.0f, 100.0f)];
[self.content addSubview:self.imageView];
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(0.0,0.0,120.0f,110.0f)];
self.content.textContainer.exclusionPaths = #[exclusionPath];
} else {
[self.imageView setImage:chosenImage];
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
Here's the problem: when i try to dismiss UIImagePickerViewController using dismissViewController, it is just ignored. However, it works fine without the two lines where i'm setting the exclusionPaths. Also, this only happens when there's already text in the UITextView. If there's no text, I can do this as many times as I want and dismissViewController works fine. I've stepped through the code and the code actually does pass through the [picker dismissViewControllerAnimated: completion: line. Also picker at that point is indeed an instance of UIImagePickerController. Anyone have idea what is going on?
[UPDATE] I've actually trimmed down the code to the following:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(0.0,0.0,120.0f,110.0f)];
self.content.textContainer.exclusionPaths = #[exclusionPath];
[picker dismissViewControllerAnimated:YES completion:nil];
}
Basically I don't even set the imageView anymore. It's just that when I try to set the exclusionPaths of a UITextView--self.content--the dismissViewControllerAnimated:completion: doesn't work. There is no error in the console.
You have to call dismissViewConroller: on self and not picker.
Better yet, call it on picker.presentingViewController like this:
[picker.presentingViewController dismissViewControllerAnimated:YES completion:nil];

pushViewController in the method didFinishPickingMediaWithInfo

When i use pushViewController i get to a blank view with a navigation button- Saved photos, and i don't know why.
I want to move to ReportViewController after selecting an image.
this is my code and it seems ok. what can possibly went wrong?
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//Geting the image from the camera
image = [info objectForKey:UIImagePickerControllerOriginalImage];
ReportViewController *imageSelectionVC = [[ReportViewController alloc] init];
[imageSelectionVC.imageReport setImage:image];
[picker pushViewController:imageSelectionVC animated:YES];
}
Just as the comments say, you have to dismiss the picker and push the new controller to your animation controller (assuming you have one)
This is the correct code:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:^{
ReportViewController *imageSelectionVC = [[ReportViewController alloc] init];
[imageSelectionVC.imageReport setImage:image];
[self.navigationController pushViewController:imageSelectionVC animated:YES];
}];
}

Using UIImagePickerController in UIView

I have button which when I click displays the camera (works perfectly)
- (IBAction)getPhoto:(id)sender
{
NSLog( #"Button clicked!" );
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
}
else { // IF the device doesn't have a camera, so use the photos album
[imagePicker setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
}
[self addSubview:imagePicker.view];
}
After which I can take an image, etc, - But when I click "use" : nothing happens here is my method : the Image picker does not dismiss itslelf : the application simply does nothing, I'm trying to take the image I just took and display it inside a CGRect on screen .
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:NULL];
UIImageView *patientImage = [[UIImageView alloc] init];
patientImage.frame = CGRectMake(625, 25, 83, 103);
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.patientImage.image = chosenImage;
}
Even when I hit the cancel button without taking an image, the imagePicker (camera interface) does not dismiss itself
Any help, advice, or guidance would be very appreciated.
You shouldn't be doing the "addSubview" thing up there but instead do:
[self presentViewController:imagePicker animated:YES completion:nil];
Instead of adding the picker as a subview, you should add the picker to a UIPopoverController.
You can then display it from within your UIView subclass by using the presentPopoverFromRect:inView:permittedArrowDirections:animated: popover method like this:
self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[self.popover presentPopoverFromRect:sender.frame inView:self permittedArrowDirections: UIPopoverArrowDirectionAny animated:YES];
Then, you can use the delegate methods to dismiss the popover using:
[self.popover dismissPopoverAnimatedYES];
The is required by the official docs:
The table indicates that on iPad, if you specify a source type of UIImagePickerControllerSourceTypePhotoLibrary or
UIImagePickerControllerSourceTypeSavedPhotosAlbum, you must present
the image picker using a popover controller, as described in
“Presenting and Dismissing the Popover” in UIPopoverController Class
Reference. If you attempt to present an image picker modally
(full-screen) for choosing among saved pictures and movies, the system
raises an exception.
On iPad, if you specify a source type of
UIImagePickerControllerSourceTypeCamera, you can present the image
picker modally (full-screen) or by using a popover. However, Apple
recommends that you present the camera interface only full-screen.
Is the getPhoto: method in the UIView subclass?
If thats the case, then it would be better to:
Move the getPhoto: code out of the UIView subclass entirely, and put it in the view's parent view controller.
Make your UIButton a property of your UIView: #property (nonatomic, strong) UIButton *myButton;
In your view controller, set the button's target & action as follows:
[myView.myButton addTarget:self action:#selector(getPhoto:) forControlEvents:UIControlEventTouchUpInside];
Finally, in your getPhoto: method, call [self presentViewController:imagePicker animated:YES completion:NULL];
Your - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info code should also go in the view controller, changing self.patientImage.image = chosenImage; to myView.patientImage.image = chosenImage;
Dont know why the cancel button is not working.
But make this change it will select the image you are clicking.
If you want an cancel event add it by creating custom UIBarButtonand add it.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImageView *patientImage = [[UIImageView alloc] init];
patientImage.frame = CGRectMake(625, 25, 83, 103);
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.patientImage.image = chosenImage;
[imagePicker.view removeFromSuperview];
}
Try:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker.view removeFromSuperview];
UIImageView *patientImage = [[UIImageView alloc] init];
patientImage.frame = CGRectMake(625, 25, 83, 103);
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.patientImage.image = chosenImage;
}
Remove the [self.addSubView:imagePicker.view]; and instead
[self presentViewController:imagePicker animated:YES completion:NULL];
You are dismissing the variable picker although picker is undefined. Instead put:
[self dismissModalViewControllerAnimated:YES completion:NULL];
and make sure its at the end of your method.

Resources