I am trying to capture an image and save it in the imageview. Below is the code.
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
NSLog(#"Hello");
[picker dismissModalViewControllerAnimated:YES];
[_Image setImage:image];
}
- (IBAction)Capture:(UIButton *)sender {
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController: cameraUI animated: YES completion:nil];
}
The problem is imagepickercontroller is not called at all!! My .h declaration is
#interface AikyaViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
I can get the camera to capture the image, but the image is not saving in my imageview. Infact it is not entering that function itself since its not NSlogging.
Should i have to link anything in the storyboard or any delegates initialisation am i missing?? Plz guide the same.
imagePickerController:didFinishPickingImage:editingInfo: is deprecated
change that method with:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"done");
[picker dismissViewControllerAnimated:YES completion:Nil];
[image setImage:[info objectForKey:#"UIImagePickerControllerEditedImage"]];
}
and try to change:
- (IBAction)Capture:(UIButton *)sender {
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
[cameraUI setDelegate:self];
[cameraUI setAllowsEditing:YES];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController: cameraUI animated: YES completion:nil];
}
Related
Im using image picker for choose photos from photo library. When i click the button it displays photo library. But when i click particular image it dismiss modalView and image won't load to view.
code:
-(void)showcamera:(id)sender{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePickerController animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
// Dismiss the image selection, hide the picker and
//show the image view with the picked image
[picker dismissModalViewControllerAnimated:YES];
UIImage *newImage = image;
}
Use this code:
in .h file
{
UIImageView *newImage;
UIImagePickerController *imagePicker;
}
in .m file
- (void)imageTaped:(UITapGestureRecognizer *)tapGesture {
imagePicker=[[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.allowsEditing =NO;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
//release picker
[picker dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//set image
newImage = [[UIImageView alloc] initWithImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
[self.view addSubview:mewImage];
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
imgPicked = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
[imagePickerController dismissModalViewControllerAnimated:YES];
}
}
Declare this in .h
UIImage *imgPicked;
in ViewDidLoad
imgPicked = [[UIImage alloc]init];
Best Way is Add Your Image to UIImageView,
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImageView *ImgView = [[UIImageView alloc] initWithFrame:CGRectMake(#"As U Want")];
ImgView.image = image;
[self.View addSubview: ImgView];
[picker dismissModalViewControllerAnimated:YES];
}
when the bar button item is tapped the popover view shows fine but it does not dismiss when a photo is selected. Am I missing something? What should i do?
-(IBAction)addPhoto:(UIBarButtonItem *)sender
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[popover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo
{
// Delete any existing image.
NSManagedObject *oldImage = imageClass.image;
if (oldImage != nil)
{
[imageClass.managedObjectContext deleteObject:oldImage];
}
// Create an image object for the new image.
NSManagedObject *myImage = [NSEntityDescription insertNewObjectForEntityForName:#"Image" inManagedObjectContext:imageClass.managedObjectContext];
imageClass.image = myImage;
// Set the image for the image managed object.
[image setValue:selectedImage forKey:#"image"];
[popover dismissPopoverAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[[picker parentViewController] dismissViewControllerAnimated:YES completion:nil];
}
The issue is that you are trying to dismiss the image picker controller but you need to dismiss the popover that it is in. Things need to be dismissed based on how they were presented.
Change:
[self dismissViewControllerAnimated:YES completion:nil];
to:
[popover dismissPopoverAnimated:YES];
Calling dismissViewControllerAnimated:completion: would be used if you have called presentViewController:animated:completion:.
To dismiss UIPopoverViewController on the selection of a photo from gallery, you need to add following lines in didFinishPickingMediaWithInfo method after picking an image.
- (void)imagePickerController:(UIImagePickerController *)imagePicker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[popover dismissPopoverAnimated:YES];
[imagePicker dismissModalViewControllerAnimated:YES];
}
You've not set delegate of imagePicker to self. Plus you need to take popover as instance variable to dismiss in delegate method.
1) in you .h you must add <UIImagePickerControllerDelegate>
2) add imagePicker.delegate=self; so your addPhoto method should look as below:
-(IBAction)addPhoto:(UIBarButtonItem *)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate=self; //add this line to your code
popover = [[UIPopoverController alloc]
initWithContentViewController:imagePicker];
[popover presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
just give the delegate UIPopoverControllerDelegate and UIImagePickerControllerDelegate in .h file
you must add UIImagePickerControllerDelegate to your corresponding .h file and also Check this Code
-(IBAction)addPhoto:(UIBarButtonItem *)sender
{
UIImagePickerController *imagePicker3 = [[UIImagePickerController alloc] init];
imagePicker3.delegate = self;
imagePicker3.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker3.allowsEditing = YES;
imagePicker3.mediaTypes = [NSArray arrayWithObject:#"public.image"]
[self presentModalViewController:imagePicker3 animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissModalViewControllerAnimated:YES];
_imageselected.image=[info valueForKey:UIImagePickerControllerEditedImage];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissModalViewControllerAnimated:YES];
}
In case someone is still experiencing this infamous error when using UIImagePickerController:
Assigning to 'id <UINavigationControllerDelegate,UIImagePickerControllerDelegate>' from incompatible type 'DetailViewController *const __strong'
try to move
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
to the
- (void)viewDidLoad{
//do your stuff
}
and don't forget to include the delegate protocol definitions (<UINavigationControllerDelegate,UIImagePickerControllerDelegate>) in the corresponding .h file.
This solved my problem, hope it's gonna help someone else. Don't know if its the best solution, but worked for me in a similar case.
i have view controller which contains a button which show the image library ,if the user tap the image,the select image will display in the same view with the button ,but i want to show this image in another class ?(view controller).my code is
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if((UIButton *) sender == choosePhotoBtn) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
it shows the image in the same view,but my need is to show it in another view.How to dod this.
Store the Image in a UIImage object and pass it to the viewController you want
In your Current View Controller
.h declare the a UIImage object
UIImage *selectedImage;
In .m file
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
selectedImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
imageView.image = selectedImage;
}
Pass it to the viewController you want
YourViewController *viewObj = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:[NSBundle mainBundle]];
viewObj.imageToBePassed = selectedImage;
//You should synthesize this object imageToBePassed
[self viewObj animated:YES];
[viewObj release];
I have 3 UIImageViews for which I want to load an image.
For 1 image this is not a problem, but how do I scale to 3?
The code below is what I have right now.
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
[my_image_1 setImage:image forState:UIControlStateNormal];
}
Have you looked at this or this? They seem to be pointing to this resource.
Edit
Based on your comment, I suggest you declare an ivar imageViewToSet in your class and alter your methods in the problem as below,
-(IBAction) getPhoto:(id) sender {
// Add code to identify the sender(button) via tags or outlets and set imageViewToSet to the mapped imageView through a switch or if statement.
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
imageViewToSet.image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
I have a uibutton to trigger an iphone's camera to grab an image, and I have a uiimageView in same view to show captured image, but it not showing anything after capturing.
Any solution?
here's my code:
(IBAction)grabImage:(id)sender{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController: picker animated:YES];
[picker release];
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
cameraImageView.image = image;
[picker dismissModalViewControllerAnimated:YES];
}