码迷,mamicode.com
首页 > 其他好文 > 详细

UIPopverController,UIImagePickerController,图片选取。

时间:2014-07-16 20:40:53      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:des   style   color   使用   文件   os   

/*使用UIImagePickerController和UIPopverController, 先在头文件中设置要遵循的协议:<UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIPopoverControllerDelegate>*/

@implementation PickerVC

{

    /*UIPopoverController must use the global variable*/

    UIPopoverController *_popController;

     UIBarButtonItem *_popbbi;

    UIImageView *_imageView;

 } 

#pragma mark - UIImagePickerControllerDelegate

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    /*

     1.implicit conversion of Objective-C pointer type ‘NSDictionary *‘to C pointer type ‘CFTypeRef‘(asa‘const void‘*)required a bridged cast

     2.Use __bridge to convert directly(no change in ownership)

     3.void CFShow (CFTypeRef obj);Prints a description of a Core Foundation object to stderr.

     */

    CFShow((__bridge CFTypeRef)(info));

     _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 300, 300, 300)];

    _imageView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"];

    [self.view addSubview:_imageView]; 

    //如果是在iPad中使用的UIPopverController,则下面这个方法没有被调用。

    [self dismissViewControllerAnimated:YES completion:^{

        printf("UIImagePickerControllerEditedImage:edited chose!\n");

    }];

}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    [picker dismissViewControllerAnimated:YES completion:^{

        printf("dismissViewController!\n");

    }];

}

-(void)pickImage:(id)sender{

    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];

    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    ipc.delegate = self;

    ipc.allowsEditing = YES;//这个设置是选取图片的关键。

    //进行硬件环境判断,如果是iPad,可以使用UIPopoverController,但是如果是在iPhone环境中则不能使用UIPopverController,否则会Crash。

    if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {

        _popController = [[UIPopoverController alloc] initWithContentViewController:ipc];

        _popController.delegate = self;

        [_popController presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

        return;

    }

    //iPhone中一般使用下面这个方法就可以了。iPad也可以使用

    [self presentViewController:ipc animated:YES completion:^{

    }];

}

-(void)popbbiClick:(UIBarButtonItem *)popbbi{

    printf("click popbbi\n");

    PickImageViewController *pvc = [[PickImageViewController alloc] init];

    [self.navigationController pushViewController:pvc animated:YES];

}

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

    self.navigationItem.rightBarButtonItem = kBarButton(@"Pick", @selector(pickImage:));

    self.title = @"Image Picker";

        _popbbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(popbbiClick:)];

    self.navigationItem.leftBarButtonItem = _popbbi;

}

 =======================下面再使用一次

@implementation PickImageViewController

{

    UIImagePickerController *_pvc;

    UIPopoverController *_popController;

    UIImageView *_imageView;

    UIImage *_image;

}

 -(void)initUIItems{

    _pvc = [[UIImagePickerController alloc] init];

    _pvc.delegate = self;

    _pvc.allowsEditing = YES;//要使用选中的Medio内容需要设置这个为YES。

    _pvc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    

    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 300, 300)];

    _imageView.backgroundColor = [UIColor yellowColor];

    _imageView.userInteractionEnabled = YES;

    [self.view addSubview:_imageView];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTouched:)];

    tap.numberOfTapsRequired = 2;

    [_imageView addGestureRecognizer:tap];

}

-(void)tapTouched:(UIGestureRecognizer *)tap{

     if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {

        UIPopoverController *popvc = [[UIPopoverController alloc] initWithContentViewController:_pvc];

        popvc.delegate = self;

        /*这里需要注意,对局部UIPopoverController对象popover我们赋给了一个全局的UIPopoverController对象popoverController。而不能直接调用popover。因为在popover对象还可见时,是不能够被释放的。

         Popover controllers are for use exclusively on iPad devices. Attempting to create one on other devices results in an exception. */

        _popController = popvc;

        [_popController presentPopoverFromRect:CGRectMake(100, 100, 400,400) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

        return;

    }

    /*在iPhonez中可以直接使用下面的方法,但是在iPad中需要使用到上面的UIPopoverController,但是记住还是需要ImagePicker的代理方法,否则选不到图片,其实不同点就在上面这一点上。*/

    [self presentViewController:_pvc animated:YES completion:^{

        printf("PresentViewController:  open ImagePicker menu!\n");

    }];

}

/*下面的代理方法依然不变!*/

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    _image = (UIImage *)[info objectForKey:@"UIImagePickerControllerOriginalImage"];

    _imageView.image = _image;

    [self dismissViewControllerAnimated:YES completion:^{

        printf("Picker choose the oneImage and dismissViewController\n");

    }];

}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

    [self dismissViewControllerAnimated:YES completion:^{

        printf("ImagePicker menu DismissViewController\n");

    }];

}

=======

对于iPad来说,可以选择UIPopverController来使用UIImagePickerController,

因为iPad的大屏关系,这样使用其实有更好的用户体验,另外注意代理设置,可编辑属性设置,

    _pvc.allowsEditing = YES;//要使用选中的Image需要设置这个为YES。

    ipc.allowsEditing = YES;//这个设置是选取图片的关键。

 注意UIPopverController只能再iPad环境下使用。

     if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {}else Crash~

 

 

UIPopverController,UIImagePickerController,图片选取。,布布扣,bubuko.com

UIPopverController,UIImagePickerController,图片选取。

标签:des   style   color   使用   文件   os   

原文地址:http://www.cnblogs.com/longtaozi/p/3836641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!