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.
Related
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];
}
EDIT : I am using UIStoryBoard.
I have presented like this:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //| UIImagePickerControllerSourceTypeSavedPhotosAlbum ;
imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,nil];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
imagePicker.allowsEditing = YES;
[self.navigationController presentViewController:imagePicker animated:YES completion:^{
}];
}
}
Now when dissmissed
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = info[UIImagePickerControllerEditedImage];
NSLog(#"Image : %#",image);
[self dismissViewControllerAnimated:YES completion:^{
}];
}
Now view becomes like this as shown in fiqure :
EDIT : view gets pushed up to 20px when dissmissed.
EDIT : This is only in iOS 6.0 only
The reason was i was setting frame of view controller which is in protrait mode as its previous view was in landscape mode.
self.view.bounds = CGRectMake(0,0,...,...);
Whenever imagepicker dissmiss got called it moved to original position as mentioned.
Now changed in structure for orientation without setting self.view frame externally solved my problem.
If that blue part of view is custom UIView then you should check the auto resizing mask for that view. You will find the problem.
I have similar problem, and in my case 20 pixels was the status bar height.
So, try to set status bar visibility to NO before showing imagePicker and YES when it finished (in delegate methods).
something like this:
[UIApplication sharedApplication].statusBarHidden = YES;
[self.navigationController presentViewController:imagePicker animated:YES completion:^{
}];
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// ... your code here
[UIApplication sharedApplication].statusBarHidden = NO;
[self dismissViewControllerAnimated:YES completion:^{
}];
}
I had similar issue on iOS 8.2. After picking video using UIImagePickerController the frame is increased by 20px, top area of view controller is looking good, but the bottom is cut off.
The solution is:
-(void)openPicker {}
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//configure image picker here
[self presentViewController:picker animated:YES completion:NULL];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(showStatusBar) userInfo:nil repeats:NO];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
-(void)showStatusBar {
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
});
}
I'm trying to present an UIImagePickerController. It works fine on simulator, but after few times of presenting, I get an error in the console.
More than maximum 5 filtered album lists trying to register.
This will fail.
Here is the code:
- (IBAction)imageSelectorPressed:(id)sender
{
UIImagePickerController* picker = [[[UIImagePickerController alloc] init] autorelease];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
// Set source to the Photo Library
picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
}
picker.delegate = self;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:picker];
self.imagePopOverController = popover;
[self.imagePopOverController setDelegate:self];
[self.imagePopOverController presentPopoverFromRect:[kepekBtn frame] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[popover release];
}
-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
[popoverController dismissPopoverAnimated:YES];
[self setImagePopOverController:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self.imagePopOverController dismissPopoverAnimated:NO];
[image setImage:[info objectForKey:#"UIImagePickerControllerOriginalImage"]];
picker = nil;
[self setImagePopOverController:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self.imagePopOverController dismissPopoverAnimated:NO];
picker = nil;
[self setImagePopOverController:nil];
}
UPDATE: I've checked the contact app on the iPad. Edited a person, and it has a same error in the console log. BUT it is not crashing, while my app is. So the proper question: How can I fix it?
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've got a very simple app where the user selects a UIImageView and presses a button to take a photo with the camera. The image is then returned and displayed in the UIImageView.
However, since the UIImageViews are sharing the same delegate, when there's already an image in one of the UIImageViews, and I go to take a photo to be placed in another, the previous UIImageView is replaced with empty content (i.e. no image). I guess this is because they're sharing the same delegate. Is there any way I can essentially copy the image instead of referencing the delegate version of it?
Here's some sample code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[picker.parentViewController dismissModalViewControllerAnimated:YES];
if (topView == YES)
{
NSLog(#"topView = %i", topView);
imageView.image = image;
}
else {
NSLog(#"topView = %i", topView);
imageView2.image = image;
}
}
Thanks!
Edit: Here's the code that gets called by the IBAction on the button presses
- (IBAction) pushPick {
topView = YES;
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
}
- (IBAction) pushPick2 {
topView = NO;
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
}
Something like this:
UIImage * newImage = [UIImage imageWithCGImage:[image CGImage]];
use
[image copy];
But remember that you will need to release the object on dealloc