How to open UIImagePopOverController in iPad

How to open UIImagePopOverController in iPad


Today I am going to describe to open UIImagePopOverController in iPad.
If you are iOS developer then very well know that you can not directly open UIImagePopOverController.
For that you need to take help of UIPopOverController
Define in the source file .h
?
1
2
3
4
5
6
7
8
9
10
11
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIPopoverControllerDelegate, UIImagePickerControllerDelegate>
{
    UIPopoverController *popoverController;
    IBOutlet UIButton *yourBtn;
    IBOutlet UIImageView *img;
}
- (IBAction) showCameraUI;
@end
Design your interface in Xib in which you need to add one UIImageView and also add one UIButton and give it’s IBAction and IBOutlet according you define previously
Now implement the code as follow
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
- (IBAction) showCameraUI {
    BOOL hasCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
    UIImagePickerController* picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = hasCamera ? UIImagePickerControllerSourceTypeCamera :    UIImagePickerControllerSourceTypePhotoLibrary;
    if (self.popoverController != nil) {
        [self.popoverController dismissPopoverAnimated:YES];
        self.popoverController=nil;
    }
    self.popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
    CGRect popoverRect = [self.view convertRect:[yourBtn frame]
                                       fromView:[yourBtn superview]];
     
    popoverRect.size.width = MIN(popoverRect.size.width, 100) ;
    popoverRect.origin.x = popoverRect.origin.x;
     
    [self.popoverController
     presentPopoverFromRect:popoverRect
     inView:self.view
     permittedArrowDirections:UIPopoverArrowDirectionAny
     animated:YES];
     
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissModalViewControllerAnimated:YES];
    UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
     img.image= image;
    if (popoverController != nil) {
        [popoverController dismissPopoverAnimated:YES];
        popoverController=nil;
    }
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
    [picker dismissModalViewControllerAnimated:YES];
    if (popoverController != nil) {
        [popoverController dismissPopoverAnimated:YES];
        popoverController=nil;
    }
}
- (void)pickerDone:(id)sender
{
    if (popoverController != nil) {
        [popoverController dismissPopoverAnimated:YES];
        popoverController=nil;
    }
}
Check example ImagePop
cd:http://technopote.com/how-to-open-uiimagepopovercontroller-in-ipad/

Comments

Popular posts from this blog

รู้จักกับ Breakpoints ใน Responsive Web Design

IS-IS & OSPF

RIP Routing Information Protocol