#import<UIKit/UIKit.h>
@interface ViewController : UIViewController
@property(strong,nonatomic) UIButton *btn;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
? ? [super viewDidLoad];
? ? self.btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
?self.btn.frame = CGRectMake(100, 50, 150, 44);? ? self.btn.backgroundColor = [UIColor blackColor];
[self.btn setTitle:@"選擇按鈕" forState:UIControlStateNormal];? ? [self.btn setTintColor:[UIColor orangeColor]];
?[self.btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
?[self.view addSubview:self.btn];? ? ? ? ? ? ? ?
}
-(void)clickBtn:(UIButton *)sender
{
? ? UIAlertController *alertcontroller = [UIAlertController alertControllerWithTitle:@"請選擇" message:nil preferredStyle:UIAlertControllerStyleActionSheet];? ? ? ? UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
? ? ? ? UIAlertAction *oKAction = [UIAlertAction actionWithTitle:@"相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
? ? ? ? [self photo];
? ? }];
? UIAlertAction *xiangji = [UIAlertAction actionWithTitle:@"相機" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
? ? ? ? [self camera];
? ? }];
? ? ? ? UIAlertAction *localVideo = [UIAlertAction actionWithTitle:@"本地視頻" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
? ? ? ? [self localVideo];
? ? }];
? ? ? UIAlertAction *shotVideo = [UIAlertAction actionWithTitle:@"拍攝視頻" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
? ? ? ? [self shotVideo];
? ? }];
? ? ? ? //將UIAlertAction添加到UIAlertController上
? ? [alertcontroller addAction:oKAction];
? ? [alertcontroller addAction:xiangji];
? ? [alertcontroller addAction:localVideo];
? ? [alertcontroller addAction:shotVideo];
? ? [alertcontroller addAction:cancelAction];
? ? ? //模態視圖顯示
? [self presentViewController:alertcontroller animated:YES completion:nil];
? }
//相冊
-(void)photo{
? ? /**? ? UIImagePickerControllerSourceTypePhotoLibrary ,//來自圖庫? ? UIImagePickerControllerSourceTypeCamera ,//來自相機? ? UIImagePickerControllerSourceTypeSavedPhotosAlbum //來自相冊? ? */? ? ? ?
//初始化UIImagePickerController 類
? ? UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
? ? //指定代理
? ? imagePicker.delegate = self;
? //允許用戶進行編輯
? ? imagePicker.allowsEditing = YES;
? ? imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
? ? //以模式視圖控制器的形式顯示
? ? [self presentViewController:imagePicker animated:YES completion:nil];
}
//相機
-(void)camera
{
? ? //判斷是否可以打開相機,模擬器此功能無法使用
? ? if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
? ? ? UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"錯誤" message:@"沒有攝像頭" preferredStyle:UIAlertControllerStyleAlert];
? ? ?? UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleDefault handler:nil];
? ? ? ? [alert addAction:okAction];? ??
? ? ? ? ? [self presentViewController:alert animated:YES completion:nil];??
? // return;
? ? }else{
? ? ? UIImagePickerController *imageP = [[UIImagePickerController alloc] init];
? ? ? ? imageP.delegate = self;
? ? ? ? imageP.allowsEditing = YES;
? ? ? ? imageP.sourceType = UIImagePickerControllerSourceTypeCamera;
? ? ? ? ? ? ? [self presentViewController:imageP animated:YES completion:nil];
? ? }
? ? }
//本地視頻
-(void)localVideo{
? ? UIImagePickerController *imagePV = [[UIImagePickerController alloc] init];
? ? ? ? imagePV.delegate = self;
? ? ? imagePV.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
? ? //自定媒體類型
? imagePV.mediaTypes = @[@"public.movie"];
? ? ? [self presentViewController:imagePV animated:YES completion:nil];
}
//拍攝視頻
-(void)shotVideo{
? ? UIImagePickerController *imagePS = [[UIImagePickerController alloc] init];
? ? ? imagePS.delegate = self;
? ? ? imagePS.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;? ? ? ? imagePS.mediaTypes = @[@"public.movie"];
? ? ? ? [self presentViewController:imagePS animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
/**
選取的信息都在info中,info 是一個字典。
字典中的鍵:
NSString *const? UIImagePickerControllerMediaType ;指定用戶選擇的媒體類型(文章最后進行擴展)
NSString *const? UIImagePickerControllerOriginalImage ;原始圖片
NSString *const? UIImagePickerControllerEditedImage ;修改后的圖片
NSString *const? UIImagePickerControllerCropRect ;裁剪尺寸
NSString *const? UIImagePickerControllerMediaURL ;媒體的URL
NSString *const? UIImagePickerControllerReferenceURL ;原件的URL
NSString *const? UIImagePickerControllerMediaMetadata;當數據來源是照相機的時候這個值才有效
*/
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]) {
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
//如果是拍攝的照片
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
//保存在相冊
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-100)/2, 200, 100, 100)];
imageV.image = image;
imageV.layer.cornerRadius = imageV.frame.size.width/2;
//設置短片界限
imageV.clipsToBounds = YES;
[self.view addSubview:imageV];
}
else if ([mediaType isEqualToString:@"public.movie"]){
//獲取視圖的url
NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];
//播放視頻
NSLog(@"%@",url);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end