前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >机试题:地图定位、拍照并显示、录制视频并播放

机试题:地图定位、拍照并显示、录制视频并播放

作者头像
tandaxia
发布2018-09-27 15:36:54
5700
发布2018-09-27 15:36:54
举报
文章被收录于专栏:谈补锅

  这两天参加面试,有个公司先出了机试题,然后才能进入下一步,机试题大意是要求实现:地图定位、拍照并显示照片、录制视频并且播放视频三个小功能。

先上我的效果图:

1、地图定位关键代码(ios8后,开启地图定位需要在工程文件里面设置描述):

代码语言:javascript
复制
//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;

2、拍照和录制视频关键代码: 

代码语言:javascript
复制
#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];
}

DEMO下载:

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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-05-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档