Demo 实现的功能:点击头像进入相册,选择相册中的图片后头像变为选中的图片
#import "ViewController.h"
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
UIImageView * _headerImageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createUI];
}
-(void)createUI{
_headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
_headerImageView.layer.cornerRadius = 75;
_headerImageView.layer.masksToBounds = YES;
//设置边框
_headerImageView.layer.borderColor = [UIColor blackColor].CGColor;
_headerImageView.layer.borderWidth = 3;
_headerImageView.center = self.view.center;
_headerImageView.backgroundColor =[UIColor redColor];
_headerImageView.userInteractionEnabled = YES;
[self.view addSubview:_headerImageView];
//点击手势
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeImage:)];
[_headerImageView addGestureRecognizer:tap];
}
#pragma mark - 点击手势
-(void)changeImage:(UITapGestureRecognizer *)tap{
//创建actionSheet 供用户选择
//ios9之后的用法
//UIActionSheet * action = [[UIActionSheet alloc] initWithTitle:@"提示" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"相册", nil];
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"选择照片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//添加相机选项
[alert addAction:[UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//判断有无相机设备
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[self loadImageWithType:UIImagePickerControllerSourceTypeCamera];
}else{
NSLog(@"未找到相机");
}
}]];
//添加相册选项
[alert addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//访问相册
[self loadImageWithType:UIImagePickerControllerSourceTypePhotoLibrary];
}]];
//取消按钮
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}
-(void)loadImageWithType:(UIImagePickerControllerSourceType)type{
//实例化
UIImagePickerController * picker= [[UIImagePickerController alloc] init];
//设置图片来源,相机或者相册
picker.sourceType = type;
picker.delegate = self;
//设置后续是否可编辑
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:nil];
}
#pragma mark - 代理方法
//完成选择图片之后的回调 也就是点击系统自带的choose按钮之后调用的方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(@"选择照片");
//多选???
_headerImageView.image = info[UIImagePickerControllerEditedImage];
self.view.backgroundColor = [UIColor colorWithPatternImage:info[UIImagePickerControllerEditedImage]];
[self dismissViewControllerAnimated:YES completion:nil];
}
//取消选择
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}
视频播放demo
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import "CustomerMoviePlayer.h"
#import "CustomerAVPlayerController.h"
//ios9之后的视频播放
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self createUI];
}
-(void)createUI{
NSArray * arr = [NSArray arrayWithObjects:@"本地播放",@"网络播放",@"强制横屏", nil];
for (int i = 0; i < arr.count; i++) {
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(i*SCREEN_WIDTH/3, 200, SCREEN_WIDTH/3-20, 40);
[btn setTitle:arr[i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
btn.tag = 100+i;
[btn addTarget:self action:@selector(onClickListener:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
}
-(void)onClickListener:(UIButton *)btn{
switch (btn.tag) {
case 100:
{
//本地播放
NSURL * url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"MovieTest" ofType:@"mp4"]];
[self playOnIOS9WithURL:url];
}
break;
case 101:
{
//网络播放
NSURL * url = [NSURL URLWithString:@"http://video.szzhangchu.com/1442391674615_6895658983.mp4"];
[self playWithUrl:url];
}
break;
case 102:
{
NSURL * url = [NSURL URLWithString:@"http://video.szzhangchu.com/1442391674615_6895658983.mp4"];
//强制横屏播放
/*
CustomerMoviePlayer *vc = [[CustomerMoviePlayer alloc] initWithContentURL:url];
[vc.moviePlayer prepareToPlay];
[vc.moviePlayer play];
[self presentViewController:vc animated:YES completion:nil];
*/
//ios9之后实现
[self newForceOritation:url];
}
break;
default:
break;
}
}
-(void)playWithUrl:(NSURL *)url{
//实例化播放器
MPMoviePlayerViewController * playVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
//设置播放资源的来源
playVC.moviePlayer.movieSourceType =MPMovieSourceTypeFile;
//设置全屏播放
playVC.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
//播放之前 进行预播放
[playVC.moviePlayer prepareToPlay];
[playVC.moviePlayer play];
//需要退出播放控制器
[self presentViewController:playVC animated:YES completion:nil];
}
#pragma mark - ios9之后的视频播放
//ios9之后,系统原生的播放器 MPMoviePlayerViewController被废弃掉 有Avkit框架下的AVPlayerViewController代替,同时结合AVFoundation框架下的AVPlayer使用
-(void)playOnIOS9WithURL:(NSURL *)url{
AVPlayerViewController * playVC = [[AVPlayerViewController alloc] init];
//设置播放资源
AVPlayer * player = [AVPlayer playerWithURL:url];
playVC.player = player;
//推出播放的视图控制器
[self presentViewController:playVC animated:YES completion:nil];
}
//强制全屏
-(void)newForceOritation:(NSURL *)url{
CustomerAVPlayerController * playVC =[[CustomerAVPlayerController alloc] init];
AVPlayer * player =[AVPlayer playerWithURL:url];
playVC.player = player;
[self presentViewController:playVC animated:YES completion:nil];
}
//强制横屏 重写两个控制器类
@interface CustomerMoviePlayer : MPMoviePlayerViewController
@interface CustomerAVPlayerController : AVPlayerViewController
//重写系统的两个方法
-(BOOL)shouldAutorotate{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
iOS开发 - 访问相机相册
最新推荐文章于 2024-08-25 04:01:03 发布