在IOS上很多應用都需要調用系統的攝像頭以及相冊的權限,IOS也給我們提供了接口。
UIImagePickerController繼承自UINavigationController,所以我們不可以在攝像頭調用是添加新的界面,會導致導航控制器出錯,但是我們可以獲取攝像頭界面的所有按鈕,對按鈕進行一些自定義操作。
注意:
攝像頭拍照界面,以及拍照完成重拍還有確認選擇等操作都是在一個試圖控制器上完成的,我們不要誤以爲是兩個界面。爲什麼可以確定這件事呢?因爲我們可以通過方法在該界面ViewController裏面獲取到操作過程中遇到的所有按鈕。
拍照界面視圖層級關係如下:
如果要獲取PLCropOverlay,你就要先獲取PLCameraView的視圖。
下面我們來看例子:
ViewController.h:
<pre name="code" class="objc">#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@end
ViewController.m:
<pre name="code" class="objc">//
// ViewController.m
// 攝像頭
//
// Created by admin on 14-10-18.
// Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(80, 80, 160, 160);
imageView.backgroundColor = [UIColor grayColor];
[self.view addSubview:imageView];
self.imageView = imageView;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(110 , 320, 100, 40);
[button setTitle:@"打開相冊" forState:UIControlStateNormal];
[button addTarget:self action:@selector(openPics) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2.frame = CGRectMake(110, 420, 100, 40);
[button2 setTitle:@"打開相機" forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(openCamera) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
}
// 打開相機
- (void)openCamera {
// UIImagePickerControllerCameraDeviceRear 後置攝像頭
// UIImagePickerControllerCameraDeviceFront 前置攝像頭
BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
if (!isCamera) {
NSLog(@"沒有攝像頭");
return ;
}
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
// 編輯模式
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:^{
}];
}
// 打開相冊
- (void)openPics {
//實例化照片選擇控制器
UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc]init];
//設置照片源
[imagePickerVC setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];//圖片庫
//設置照片是否允許編輯
[imagePickerVC setAllowsEditing:YES];
//設置代理
[imagePickerVC setDelegate:self];
//顯示控制器,由當前試圖控制器複雜展示照片選擇控制器
[self presentViewController:imagePickerVC animated:YES completion:nil];
}
// 選中照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//照片選擇完成
NSLog(@"%@", info);
// UIImagePickerControllerOriginalImage 原始圖片
// UIImagePickerControllerEditedImage 編輯後圖片
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
[self.imageView setImage:image];
[self dismissViewControllerAnimated:YES completion:nil];//completion之後是關閉之後做的事情
}
// 取消相冊
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
-(UIView *)findView:(UIView *)aView withName:(NSString *)name{
Class cl = [aView class];
NSString *desc = [cl description];
if ([name isEqualToString:desc])
return aView;
for (NSUInteger i = 0; i < [aView.subviews count]; i++)
{
UIView *subView = [aView.subviews objectAtIndex:i];
subView = [self findView:subView withName:name];
if (subView)
return subView;
}
return nil;
}
-(void)addSomeElements:(UIViewController *)viewController{
UIView *PLCameraView=[self findView:viewController.view withName:@"PLCameraView"];
UIView *bottomBar=[self findView:PLCameraView withName:@"PLCropOverlayBottomBar"];
UIImageView *bottomBarImageForSave = [bottomBar.subviews objectAtIndex:0];
UIButton *retakeButton=[bottomBarImageForSave.subviews objectAtIndex:0];
[retakeButton setTitle:@"重拍" forState:UIControlStateNormal]; //左下角按鈕
[retakeButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
UIButton *useButton=[bottomBarImageForSave.subviews objectAtIndex:1];
[useButton setTitle:@"確認選擇" forState:UIControlStateNormal]; //右下角按鈕
}
- (void)click:(UIButton *)btn
{
NSLog(@"**********");
// [btn setTitle:@"重寫" forState:UIControlStateNormal];
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
[self addSomeElements:viewController];
}
@end