Assign the taken image to image view.image - ios

I have image view which is hidden initially. When I click on TakePhotoButton and then it takes images and assign it to the image view. then image view is visible.
When I debug my code, I always get my productImg is nil, even though chosenImage is not nil.
#property (strong, nonatomic) IBOutlet UIImageView *productImg;
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.productImg.hidden=NO;
self.productImg.image=chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}

Try this out:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
__block MyViewController *aBlockSelf = self; // Replace MyViewController with your View Controller
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[picker dismissViewControllerAnimated:YES completion:^{
aBlockSelf.productImg.hidden = NO;
aBlockSelf.productImg.image = chosenImage;
}];
}

you use following code It may be helpful to you.
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
{
UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];
self.productImg.hidden=NO;
self.productImg.image=chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}

Why do you have to hide it initially.Since the image will be null it will not be seen any how.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage;
chosenImage= info[UIImagePickerControllerOriginalImage];
[self.productImg setImage:chosenImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
}

Related

getting null when printing an image in didFinishPickingMediaWithInfo method

I am trying to pick an image from photo library and for that I used picker controller, but when I am trying to print the image in didFinishPickingMediaWithInfo method I am getting a null response. I used protocols also in my .h file.
- (IBAction)choosePhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
NSLog(#"choose the img");
[self presentViewController:picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.imageView.image = info[UIImagePickerControllerEditedImage];
NSLog(#"imgae is %#",self.imageView.image);
[picker dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
// here you can check that image is null or not
[Btn_ImagePicker setImage:chosenImage forState:UIControlStateNormal];
[picker dismissViewControllerAnimated:YES completion:NULL];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
For the full detail kindly print NSLog(#"%#",info); info contains full details.

Assigning image from photo album to a variable Objective-c

I am getting image from a photo album with this code:
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];}
Please Could anyone help me to assign this image to a variable? I want to blur it later using stackblur:
UIImage *newIma=[oldIma stackBlur:radius];
Thanks in advance!
Set the UIImage when you're getting the image that the user has selected.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
// Create the UIImage in your #implementation and set it here
myImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
myImageView.image = myImage;
}

Taking a photo and saving it to the Camera Roll

I'm having a problem where I just want a simple solution to take a picture and then save it in my app. However so far all I can do is take the photo have it load in a UIImageView, however it does not save in the Camera Roll.
- (IBAction)takePhoto:(UIButton *)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[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];
}
Any help regarding this would be greatly appreciated. Believe me, I really need it.
You will want to do this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
UIImageWriteToSavedPhotosAlbum(chosenImage, nil, nil, nil);
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Swift 4
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if(info[UIImagePickerControllerMediaType] as! String == "public.image"){
guard let curImage:UIImage = info[UIImagePickerControllerOriginalImage] as? UIImage else{
return
}
UIImageWriteToSavedPhotosAlbum(curImage, nil, nil, 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];
}];
}

imageView doesn't show image from UIImagePickerController

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{

Resources