1)打开相册
- (void)openPicture
{
UIImagePickerController *ipc = [[UIImagePickerController alloc]init];
[ipc setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
ipc.delegate = self;
[self presentViewController:ipc animated:YES completion:nil];
}
//其他类型
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary,//相册
UIImagePickerControllerSourceTypeCamera,//相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum//已保存图片
}
2)打开相册后导航栏设置
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[viewController.navigationController.navigationBar setBackgroundImage:[[UIImage imageNamed:@"bg_navg.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10]forBarMetrics:UIBarMetricsDefault];
UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];
[leftBtn setTitle:@" " forState:UIControlStateNormal];
leftBtn.enabled = NO;
[leftBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
[viewController.navigationItem setLeftBarButtonItem:leftItem animated:YES];
UIButton *cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 30)];
[cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
[cancelBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
[cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithCustomView:cancelBtn];
[viewController.navigationItem setRightBarButtonItem:cancelItem animated:YES];
[cancelBtn addTarget:self action:@selector(imagePickerControllerDidCancel:) forControlEvents:UIControlEventTouchUpInside];
viewController.title = @"照片";
NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,[UIColor clearColor],NSForegroundColorAttributeName,NSForegroundColorAttributeName,[UIFont systemFontOfSize:18], nil];
[viewController.navigationController.navigationBar setTitleTextAttributes:dic];
}
3)点击图片回调方法(UIImagePickerControllerDelegate)
//点击相册的图片做什么
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//获取到点击的图片
photoImage = [info objectForKey:UIImagePickerControllerOriginalImage];
self.scanView.photoImgV.image = photoImage;
//解析图片里的条形码
readC = [[ZBarReaderController alloc]init];
readC.readerDelegate = self;
ZBarSymbol* symbol = nil;
self.scanResult = @"";
for (symbol in [readC scanImage:photoImage.CGImage]) {
const zbar_symbol_t *symbol1 = symbol.zbarSymbol;
self.scanResult = [NSString stringWithUTF8String: zbar_symbol_get_data(symbol1)];
}
//返回
[self dismissViewControllerAnimated:YES completion:^{
readC= nil;
if(self.scanResult.length == 0)
{
[StaticTools showHUDInView:self.view HudType:MBProgressHUDModeText withText:@"扫描的图片无二维码"];
return ;
}
NSRegularExpression *urlRegexExp = [[NSRegularExpression alloc] initWithPattern:@"^http(s){0,1}://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$" options:0 error:nil];
if (urlRegexExp)
{
NSTextCheckingResult *checkingResult = [urlRegexExp firstMatchInString:self.scanResult options:0 range:NSMakeRange(0, [self.scanResult length])];
if (checkingResult){
[self scanURL:self.scanResult];
}
else {
[self scanText:self.scanResult];
}
}
else{
[self scanResultError:self.scanResult];
}
}];
}
4)点击取消按钮响应方法
//点击取消按钮
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self.navigationController dismissViewControllerAnimated:YES completion:^{
}];
}