打開(kāi)攝像頭顯示中文躏升,可在Info.plist添加Localizations(array)字段,添加Chinese (simplified)
//
// ViewController.m
// TestVideoAndPicture
//
// Created by albert on 2017/4/14.
// Copyright ? 2017年 albert. All rights reserved.
//
#import "ViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <MobileCoreServices/MobileCoreServices.h>
@interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *testGestureButton;
@property (weak, nonatomic) IBOutlet UIImageView *testImageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
// 選擇相冊(cè)中視頻和照片
- (IBAction)videos:(id)sender {
// 檢查相冊(cè)是否可用
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
NSLog(@"sorry, no camera or camera is unavailable.");
return;
}
// 創(chuàng)建圖像選取控制器
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
// 設(shè)置圖像選取控制器的來(lái)源模式為PhotoLibrary
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// 設(shè)置圖像選取控制器的類型為視頻和照片
imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects:(NSString*)kUTTypeMovie, (NSString*)kUTTypeImage, nil];
// 允許用戶進(jìn)行編輯
imagePickerController.allowsEditing = YES;
// 設(shè)置委托對(duì)象
imagePickerController.delegate = self;
// 以模視圖控制器的形式顯示
[self presentViewController:imagePickerController animated:YES completion:nil];
}
// 拍照
- (IBAction)takePictureButtonClick:(id)sender{
// 檢查相機(jī)模式是否可用
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSLog(@"sorry, no camera or camera is unavailable.");
return;
}
// 獲得相機(jī)模式下支持的媒體類型
NSArray *availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
BOOL canTakePicture = NO;
for (NSString *mediaType in availableMediaTypes) {
if ([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
// 支持拍照
canTakePicture = YES;
break;
}
}
// 檢查是否支持拍照
if (!canTakePicture) {
NSLog(@"sorry, taking picture is not supported.");
return;
}
// 創(chuàng)建圖像選取控制器
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
// 設(shè)置圖像選取控制器的來(lái)源模式為相機(jī)模式
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
// 設(shè)置圖像選取控制器的類型為靜態(tài)圖像
imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects:(NSString*)kUTTypeImage, nil];
// 允許用戶進(jìn)行編輯
imagePickerController.allowsEditing = YES;
// 設(shè)置委托對(duì)象
imagePickerController.delegate = self;
// 以模視圖控制器的形式顯示
[self presentViewController:imagePickerController animated:YES completion:nil];
}
// 錄像
- (IBAction)captureVideoButtonClick:(id)sender{
// 檢查相機(jī)模式是否可用
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSLog(@"sorry, no camera or camera is unavailable!!!");
return;
}
// 獲得相機(jī)模式下支持的媒體類型
NSArray *availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
BOOL canTakeVideo = NO;
for (NSString *mediaType in availableMediaTypes) {
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
// 支持?jǐn)z像
canTakeVideo = YES;
break;
}
}
// 檢查是否支持?jǐn)z像
if (!canTakeVideo) {
NSLog(@"sorry, capturing video is not supported!!!");
return;
}
// 創(chuàng)建圖像選取控制器
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
// 設(shè)置圖像選取控制器的來(lái)源模式為相機(jī)模式
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
// 設(shè)置圖像選取控制器的類型為動(dòng)態(tài)圖像
imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
// 設(shè)置攝像圖像品質(zhì)
imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
// 設(shè)置最長(zhǎng)攝像時(shí)間
imagePickerController.videoMaximumDuration = 30;
// 允許用戶進(jìn)行編輯
imagePickerController.allowsEditing = YES;
// 設(shè)置委托對(duì)象
imagePickerController.delegate = self;
// 以模式視圖控制器的形式顯示
[self presentViewController:imagePickerController animated:YES completion:nil];
}
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{
if (!error) {
NSLog(@"picture saved with no error.");
}else{
NSLog(@"error occured while saving the picture%@", error);
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
// 打印出字典中的內(nèi)容
NSLog(@"get the media info: %@", info);
// 獲取媒體類型
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
// 判斷是靜態(tài)圖像還是視頻
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) { // 圖片
// 獲取用戶編輯之后的圖像
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
// 將該圖像保存到媒體庫(kù)中
UIImageWriteToSavedPhotosAlbum(editedImage, self,@selector(image:didFinishSavingWithError:contextInfo:), NULL);
}else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) { // 視頻
// 獲取視頻文件的url
NSURL *mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
// 創(chuàng)建ALAssetsLibrary對(duì)象并將視頻保存到媒體庫(kù)
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary writeVideoAtPathToSavedPhotosAlbum:mediaURL completionBlock:^(NSURL *assetURL, NSError *error) {
if (!error) {
NSLog(@"captured video saved with no error.");
}else{
NSLog(@"error occured while saving the video:%@", error);
}
}];
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end