Device
#import "AppDelegate.h"
#import "RootViewController.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
RootViewController *root =[[RootViewController alloc] init];
UINavigationController *nav =[[UINavigationController alloc] initWithRootViewController:root];
[root release];
self.window.rootViewController = nav
;
[nav release];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
// RootViewController.h
// Device
//
// Created by 张国锋 on 15/7/23.
// Copyright (c) 2015年 张国锋. All rights reserved.
// #import <UIKit/UIKit.h>
//带有音频播放器的framework
#import <AVFoundation/AVFoundation.h> @interface RootViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate,AVAudioPlayerDelegate> @end //
// RootViewController.m
// Device
//
// Created by 张国锋 on 15/7/23.
// Copyright (c) 2015年 张国锋. All rights reserved.
// #import "RootViewController.h"
//此framework中带有系统预置的多媒体常量参数
#import <MobileCoreServices/MobileCoreServices.h>
#import "ImageTool.h"//对图片进行压缩处理的工具类
#import <MediaPlayer/MediaPlayer.h>//此framework中带有视频播放器
@interface RootViewController (){ AVAudioPlayer *_audioPlayer;//音频播放器
//带有视频播放器的控制器(能够播放mp4、avi、mov格式的视频,支持本地和远程视频的播放)
MPMoviePlayerViewController*_playController;
} @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
NSArray *titles = [NSArray arrayWithObjects:@"拍照",@"相册库",@"音频",@"视频",nil];
for (int i = 0; i<titles.count; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:[titles objectAtIndex:i] forState:UIControlStateNormal];
[btn setFrame:CGRectMake(10,70+i*60,300,50)];
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = 100+i;
[self.view addSubview:btn];
}
} #pragma mark - customMethods
- (void)btnClicked:(UIButton *)btn{
switch (btn.tag) {
case 100:
{
//拍照功能
//先判断硬件是否支持拍照功能
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[self loadImagePickerWithSourceType:UIImagePickerControllerSourceTypeCamera];
}else{
//提示用户
[self showAlertViewWithMessage:@"不支持拍照功能"];
}
}break;
case 101:
{
//调用系统相册库
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
[self loadImagePickerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}else {
[self showAlertViewWithMessage:@"无法获取相册库"];
}
}break;
case 102:
{
//音频
NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"song1" ofType:@"mp3"];
//播音频
[self playAudioWithPath:audioPath];
//[self haha]; }break;
case 103:
{
NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp4"];
[self playVideoWithPath:videoPath];
//[self hahahaha];
}break;
default:
break;
}
} //根据不同的资源参数加载不同的资源(拍照、相册库)
- (void)loadImagePickerWithSourceType:(UIImagePickerControllerSourceType)type{
//UIImagePickerController
//通过UIImagePickerController 来获取拍照和相册库资源
UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease];
//根据不同的参数加载不同的资源
picker.sourceType = type;
//设置代理
picker.delegate = self;
//是否允许对图片、视频资源进行后续处理
picker.allowsEditing = YES;
//一般情况下picker 习惯通过模态化的方式呈现到程序中
[self presentViewController:picker animated:YES completion:^{ }];
} //根据不同的提示信息来创建警告框,用于提升用户体验
- (void)showAlertViewWithMessage:(NSString *)info{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:info delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
//将警告框呈现到应用程序
[alert show];
[alert release];
} //根据音频的资源路径来播放音频
- (void)playAudioWithPath:(NSString *)audioPath{
if (audioPath.length == 0) {
NSLog(@"没有音频资源!");
return;
}
//如果有旧的播放器对象,销毁
if (_audioPlayer) {
[_audioPlayer release];
_audioPlayer = nil;
}
//创建新的播放器对象
//本地的资源路径生成url用fileURLWithPath
NSURL *url = [NSURL fileURLWithPath:audioPath];
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
//设置代理
_audioPlayer.delegate = self;
//对音频资源进行预加载
[_audioPlayer prepareToPlay];
//播放音频
[_audioPlayer play]; //[_audioPlayer stop];
}
//播视频
- (void)playVideoWithPath:(NSString *)videoPath{
if (videoPath.length == 0) {
NSLog(@"没有视频资源!");
return;
}
//可以播放本地和远程视频
NSURL *url;
if ([videoPath rangeOfString:@"http://"].location !=NSNotFound || [videoPath rangeOfString:@"https://"].location!=NSNotFound) {
url=[NSURL URLWithString:videoPath];
}else{
//本地资源路径
url = [NSURL fileURLWithPath:videoPath];
}
if (!_playController) {
//创建一个带有视频播放器的控制器
_playController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
_playController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
//通过模态化的方式呈现
[self presentViewController:_playController animated:YES completion:^{
//停掉音频播放器
[self stopAudioPlayer];
}];
//视频资源分为普通的文件资源,还有流媒体格式(.m3u8)的视频资源
//moviePlayer属性为视频播放器,指定播放的资源的类型 //播放视频
[_playController.moviePlayer play];
//通过点击done按钮后,销毁_playController
//每个应用程序有且只有一个通知中心的对象(单例),可以理解为广播站,任何对象都可以通过通知中心发送广播
//任何对象都可以通过通知中心注册成为某条广播的观察者(具有接收/收听某条广播能力的对象)
//在通知中心注册self为MPMoviePlayerPlaybackDidFinishNotification广播的观察者,一旦有其他对象发送这条广播,self就能接收到并触发playBack方法
//addObserver 添加观察者, selector 触发的方法,name:广播的名称
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBack) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
//点击done按钮->视频播放器会自动通过通知中心发送MPMoviePlayerPlaybackDidFinishNotification这条广播
//[[NSNotificationCenter defaultCenter] postNotificationName:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
}
- (void)playBack{
//在通知中心移除self对MPMoviePlayerPlaybackDidFinishNotification广播的观察
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
if (_playController) {
//停掉播放器
[_playController.moviePlayer stop];
//销毁playController
[_playController release];
_playController = nil;
}
} //停掉音频播放器,并销毁
- (void)stopAudioPlayer{
if (_audioPlayer) {
[_audioPlayer stop];
[_audioPlayer release];
_audioPlayer = nil;
}
}
#pragma mark - UIImagePickerControllerDelegate
//点击picker上的cancel按钮时,触发的方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSLog(@"cancel!!");
//实现picker的dismiss
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
//点击choose按钮触发的方法
//info 带有选中资源的信息
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//判断选中的资源的类型
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
//kUTTypeImage 系统预置的图片资源类型
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
//证明取出来的是图片
//通过字典获取选中的图片
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
//从相机中取出来的图片占的空间:(1M-2M)左右,需要对图片进行压缩处理,然后在进行后续操作
//将原图压缩成50*50的尺寸
UIImage *smallImage = [[ImageTool shareTool] resizeImageToSize:CGSizeMake(50,50) sizeOfImage:image];
self.view.backgroundColor = [UIColor colorWithPatternImage:smallImage];
}
[picker dismissViewControllerAnimated:YES completion:^{
}]; } #pragma mark - AVAudioPlayerDelegate
//当成功播放完成一首歌后,调用此方法
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
NSLog(@"successFully!!");
} //当系统级别的功能介入(来电话了),播放器被打断时,调用此方法
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player{
NSLog(@"beginInterruption!");
}
//当播放器结束被打断,调用此方法
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags{
if (player) {
//继续播放
[player play];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
//小工具
// ImageTool.h
// SystemFunction
//
// Copyright (c) 2013年 qianfeng. All rights reserved.
// #import <Foundation/Foundation.h> @interface ImageTool : NSObject //返回单例的静态方法
+(ImageTool *)shareTool; //返回特定尺寸的UImage , image参数为原图片,size为要设定的图片大小
-(UIImage*)resizeImageToSize:(CGSize)size
sizeOfImage:(UIImage*)image; //在指定的视图内进行截屏操作,返回截屏后的图片
-(UIImage *)imageWithScreenContentsInView:(UIView *)view; @end //
// ImageTool.m
// SystemFunction
//
// Copyright (c) 2013年 qianfeng. All rights reserved.
// #import "ImageTool.h"
#import <QuartzCore/QuartzCore.h> @implementation ImageTool static ImageTool *_shareImageTool =nil;
//返回单例的静态方法
+(ImageTool *)shareTool
{
//确保线程安全
@synchronized(self){
//确保只返回一个实例
if (_shareImageTool == nil) {
_shareImageTool = [[ImageTool alloc] init];
}
}
return _shareImageTool;
} -(id)init
{
self = [super init];
if (self) { }
return self;
} //在指定的视图内进行截屏操作,返回截屏后的图片
-(UIImage *)imageWithScreenContentsInView:(UIView *)view
{
//根据屏幕大小,获取上下文
UIGraphicsBeginImageContext([[UIScreen mainScreen] bounds].size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); return viewImage;
} -(UIImage*)resizeImageToSize:(CGSize)size
sizeOfImage:(UIImage*)image
{ UIGraphicsBeginImageContext(size);
//获取上下文内容
CGContextRef ctx= UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, 0.0, size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
//重绘image
CGContextDrawImage(ctx,CGRectMake(0.0f, 0.0f, size.width, size.height), image.CGImage);
//根据指定的size大小得到新的image
UIImage* scaled= UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaled;
} @end
Device的更多相关文章
- Linux系统中的Device Mapper学习
在linux系统中你使用一些命令时(例如nmon.iostat 如下截图所示),有可能会看到一些名字为dm-xx的设备,那么这些设备到底是什么设备呢,跟磁盘有什么关系呢?以前不了解的时候,我也很纳闷. ...
- Eclipse调试Android App若选择“Use same device for future launches”就再也无法选择其他设备的问题
在狂批了某供应商的多媒体控制App有多烂后,夸下海口自己要做一个也是分分钟的事.当然要做好不容易,要超过他们的烂软件还是有信心的.过程中遇到各种坑,其中之一如下 刚开始只使用一个平板进行调试,老是弹出 ...
- 设备模型(device-model)之平台总线(bus),驱动(driver),设备(device)
关于关于驱动设备模型相关概念请参考<Linux Device Drivers>等相关书籍,和内核源码目录...\Documentation\driver-model 简单来说总线(bus) ...
- xamarin.forms uwp app部署到手机移动设备进行测试,真机调试(device portal方式部署)
最近学习xamarin.刚好 手上有一个lumia 930.所以试一试把uwp app部署到手机上,并真机调试一把. 目前环境: 1.开发pc电脑是win10,版本1607.加入了insider,所以 ...
- STM32用JLINK 烧写程序时出现NO Cortex-m device found in JTAG chain现象和解决方案
现象 CPU: STM32107VC 用JLINK 烧写程序时出现NO Cortex-m device found in JTAG chain 如图无法查找到硬件就是CPU 提示1:NO Cortex ...
- “(null)” is of a model that is not supported by this version of Xcode. Please use a different device.
ios 真机运行程序就弹出这个"(null)" is of a model that is not supported by this version of Xcode. P ...
- Device Tree(二):基本概念
转自:http://www.wowotech.net/linux_kenrel/dt_basic_concept.html 一.前言 一些背景知识(例如:为何要引入Device Tree,这个机制是用 ...
- iOS 真机测试时报错:Provisioning profile "iOS Team Provisioning Profile: XXX” doesn't include the currently selected device “XXX”.
这几天因工作需要,去给客户演示iOS项目打包的过程.之前演示都是顺利的,但后来客户自己操作时打电话说遇到了问题,出现报错. 就过去看了一下,发现一个很陌生的错误提示: The operation co ...
- The network bridge on device VMnet0 is not running
The network bridge on device VMnet0 is not running. The virtual machine will not be able to communic ...
- Direct3D设备管理器(Direct3D device manager)
这几天在做dxva2硬件加速,找不到什么资料,翻译了一下微软的两篇相关文档.并准备记录一下用ffmpeg实现dxva2,将在第三篇写到.这是第一篇,英文原址:https://msdn.microsof ...
随机推荐
- Codeforces 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈
Codeforces 1107G 线段树最大子段和 + 单调栈 G. Vasya and Maximum Profit Description: Vasya got really tired of t ...
- 《精通Spring4.X企业应用开发实战》读后感第六章(使用外部属性文件)
- Storm在zookeeper上的目录结构
storm操作zookeeper的主要函数都定义在命名空间backtype.storm.cluster中(即cluster.clj文件中). backtype.storm.cluster定义了两个重要 ...
- NPM run start使用本地的http-server
在项目开发过程中,Visual Studio 2015 一个Solution中有一个前端项目 Myproject.FrontEnd,我们使用node.js, npm来进行管理 在这个项目中,有一个pa ...
- .Net开发中的@ 和 using 使用技巧
一.@符号的妙用 1.可以作为保留关键字的标识符 C#规范当中,不允许使用保留关键字(class.bool等)当作普通的标识符来命名,这时候@符号作用就体现 出来了,可以通过@符号前缀把这些保留关键字 ...
- fsck修复系统断电或非正常关机导致的系统磁盘问题
问题描述: unexpected inconsistency; run fask mannally. (i.e., without -a or -p options) fsck repaire man ...
- Ubuntu12.04安装svn1.8
先在终端执行sudo sh -c 'echo "# WANdisco Open Source Repo" >> /etc/apt/sources.list.d/WANd ...
- redis系列:通过文章点赞排名案例学习sortedset命令
前言 这一篇文章将讲述Redis中的sortedset类型命令,同样也是通过demo来讲述,其他部分这里就不在赘述了. 项目Github地址:https://github.com/rainbowda/ ...
- 【linux-command not find解决方法 】
在linux下我们经常输入某些命令时经常出现提示说:command not find 首先 当出现 command not find时肯定是环境变量的问题,所以得修改环境变量.下面我也引用一下其他牛人 ...
- GVIM安装手记
GVIM安装手记 1. 安装GIT及GVIM Downloa Git URL : https://gitforwindows.org/ Downloa GVim URL : https://www.v ...