今天在做项目的时候,需要用到系统的图片查看器。想通过调用系统方法,来去实现这个功能,但是添加了之后,跳转的时候发现。导航栏无法正常显示。点击取消也没有反应。最后自己经过多方的尝试,最终发现了几个比较好用的方法。
1.UIImagePickerViewControlle是系统内部的方法,但是在他点击取消的时候,里面用到了一些方法,详细可以看
用UIImagePickerViewController自定义相机界面
http://blog.sina.com.cn/s/blog_aedec703010181sh.html
这里定义了几个代理的方法,是可以用来实现和调用的,一般点击取消的时候会调用
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
2.如果是点击确定的话就会调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
3.需要注意的是,如果需要修改导航栏的 RightBarButtonItem还需要调用到一下这个方法
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
具体的代码实现可以这样看
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSLog(@"点击了取消按钮");
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
UIView *custom = [[UIView alloc] initWithFrame:CGRectMake(0,0,0,0)];
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithCustomView:custom];
[viewController.navigationItem setRightBarButtonItem:btn animated:NO];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
{
UIImage *originImage = [info valueForKey:UIImagePickerControllerEditedImage];
CGSize cropSize;
cropSize.width = 180;
cropSize.height = cropSize.width * originImage.size.height / originImage.size.width;
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMMddHHmmss"];
originImage = [originImage imageByScalingToSize:cropSize];
NSData *imageData = UIImageJPEGRepresentation(originImage, 0.9f);
NSString *uniqueName = [NSString stringWithFormat:@"%@.jpg",[formatter stringFromDate:date]];
NSString *uniquePath = [kDocumentsPath stringByAppendingPathComponent:uniqueName];
NSLog(@"uniquePath: %@",uniquePath);
[imageData writeToFile:uniquePath atomically:NO];
NSLog(@"Upload Image Size: %u KB",[imageData length] / 1024);
[picker dismissViewControllerAnimated:YES completion:^{
[self.delegate refleshWithfaceData:originImage];
NSLog(@"originImage==================%@",originImage);
}];
}
}