機試題:地圖定位、拍照並顯示、錄制視頻並播放


  這兩天參加面試,有個公司先出了機試題,然后才能進入下一步,機試題大意是要求實現:地圖定位、拍照並顯示照片、錄制視頻並且播放視頻三個小功能。

先上我的效果圖:

  

 

1、地圖定位關鍵代碼(ios8后,開啟地圖定位需要在工程文件里面設置描述):

//1、添加地圖視圖
    CGRect rect = [UIScreen mainScreen].bounds;
    _mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 50, rect.size.width, rect.size.height - 50)];
    [self.view addSubview:_mapView];
    //設置代理
    _mapView.delegate = self;
    
    //2、請求定位服務
    _locationManager = [[CLLocationManager alloc]init];
    if(![CLLocationManager locationServicesEnabled]||[CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse){
        [_locationManager requestWhenInUseAuthorization];
    }
    
    //3、用戶位置追蹤
    _mapView.userTrackingMode = MKUserTrackingModeFollow;
    
    //4、設置地圖類型
    _mapView.mapType = MKMapTypeStandard;
View Code

 

2、拍照和錄制視頻關鍵代碼: 

#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <MediaPlayer/MediaPlayer.h>

@interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *imgView; //顯示拍照的照片

@property (strong, nonatomic) UIImagePickerController *picker;
@property (assign, nonatomic) BOOL isChange; //是否切換拍照和錄制視頻
@property (assign, nonatomic) NSInteger currentTag; //當前tag

@property (nonatomic, strong) MPMoviePlayerController *moviePlayer;//視頻播放控制器
@property (nonatomic, strong) NSURL *movieUrl; //視頻錄制路徑
@property (nonatomic, strong) UIView *movieView; //播放器的View

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

//初始化picker
- (UIImagePickerController *)picker{
    if (_isChange || _picker == nil){
        _picker = [[UIImagePickerController alloc]init];
        _picker.sourceType = UIImagePickerControllerSourceTypeCamera;//設置image picker的來源
        _picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;//設置使用哪個攝像頭,這里設置為后置攝像頭
        
        if (self.currentTag == 1) { //錄制視頻
            _picker.mediaTypes = @[(NSString *)kUTTypeMovie];
            _picker.videoQuality = UIImagePickerControllerQualityTypeIFrame1280x720;
            _picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;//設置攝像頭模式(拍照,錄制視頻)
            //NSLog(@"錄制視頻");
        }
        else{ //拍照
            _picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
            //移除視頻播放器控件
            if (self.moviePlayer != nil){
                [self.movieView removeFromSuperview];
                self.moviePlayer = nil;
            }
        }
        _picker.allowsEditing=YES;//允許編輯
        _picker.delegate=self;//設置代理,檢測操作
    }
    return _picker;
}

//視頻播放器
-(MPMoviePlayerController *)moviePlayer{
    if (!_moviePlayer) {
        _moviePlayer = [MPMoviePlayerController new];
        _moviePlayer.view.frame = self.imgView.frame;
        _moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        self.movieView = _moviePlayer.view;
        [self.view addSubview:self.movieView];
    }
    _moviePlayer.contentURL = self.movieUrl;
    
    return _moviePlayer;
}

//拍照和視頻錄制: tag = 0表示拍照,= 1表示錄制視頻
- (IBAction)openVideo:(UIButton *)sender {
    
    if (self.currentTag != sender.tag) self.isChange = YES;
    
    self.currentTag = sender.tag;
    
    [self presentViewController:self.picker animated:YES completion:nil];
}

#pragma mark - 代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
    
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {//當是拍照時
        UIImage *image;
        //如果允許編輯則獲得編輯后的照片,否則獲取原始照片
        if (self.picker.allowsEditing) {
            image = [info objectForKey:UIImagePickerControllerEditedImage];//獲取編輯后的照片
        }else{
            image = [info objectForKey:UIImagePickerControllerOriginalImage];//獲取原始照片
        }
        [self.imgView setImage:image];//顯示照片
        //UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);//保存相片到相簿
    }
    else if([mediaType isEqualToString:(NSString *)kUTTypeMovie]){//當是錄制視頻時
        
        NSURL *url=[info objectForKey:UIImagePickerControllerMediaURL];//視頻路徑
        NSString *urlStr=[url path];
        
        //         //保存視頻到相簿
        //        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlStr)) {
        //            UISaveVideoAtPathToSavedPhotosAlbum(urlStr, self, nil, nil);//保存視頻到相簿
        //        }
        self.movieUrl =[NSURL fileURLWithPath:urlStr];
        [self.moviePlayer play];
    }
    
    // 關閉照片選擇器
    [self dismissViewControllerAnimated:YES completion:nil];
}
View Code

 

DEMO下載:

github地址:https://github.com/xiaotanit/Tan_LocationPhotoVideo

 

原文鏈接:http://www.cnblogs.com/tandaxia/p/5507154.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM