#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的更多相关文章

  1. Linux系统中的Device Mapper学习

    在linux系统中你使用一些命令时(例如nmon.iostat 如下截图所示),有可能会看到一些名字为dm-xx的设备,那么这些设备到底是什么设备呢,跟磁盘有什么关系呢?以前不了解的时候,我也很纳闷. ...

  2. Eclipse调试Android App若选择“Use same device for future launches”就再也无法选择其他设备的问题

    在狂批了某供应商的多媒体控制App有多烂后,夸下海口自己要做一个也是分分钟的事.当然要做好不容易,要超过他们的烂软件还是有信心的.过程中遇到各种坑,其中之一如下 刚开始只使用一个平板进行调试,老是弹出 ...

  3. 设备模型(device-model)之平台总线(bus),驱动(driver),设备(device)

    关于关于驱动设备模型相关概念请参考<Linux Device Drivers>等相关书籍,和内核源码目录...\Documentation\driver-model 简单来说总线(bus) ...

  4. xamarin.forms uwp app部署到手机移动设备进行测试,真机调试(device portal方式部署)

    最近学习xamarin.刚好 手上有一个lumia 930.所以试一试把uwp app部署到手机上,并真机调试一把. 目前环境: 1.开发pc电脑是win10,版本1607.加入了insider,所以 ...

  5. 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 ...

  6. “(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 ...

  7. Device Tree(二):基本概念

    转自:http://www.wowotech.net/linux_kenrel/dt_basic_concept.html 一.前言 一些背景知识(例如:为何要引入Device Tree,这个机制是用 ...

  8. iOS 真机测试时报错:Provisioning profile "iOS Team Provisioning Profile: XXX” doesn't include the currently selected device “XXX”.

    这几天因工作需要,去给客户演示iOS项目打包的过程.之前演示都是顺利的,但后来客户自己操作时打电话说遇到了问题,出现报错. 就过去看了一下,发现一个很陌生的错误提示: The operation co ...

  9. 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 ...

  10. Direct3D设备管理器(Direct3D device manager)

    这几天在做dxva2硬件加速,找不到什么资料,翻译了一下微软的两篇相关文档.并准备记录一下用ffmpeg实现dxva2,将在第三篇写到.这是第一篇,英文原址:https://msdn.microsof ...

随机推荐

  1. WCF服务用户名密码访问

    有2种方式, 第一直接在程序中指定用户名密码,配置调用 private void BtnSearch_Click(object sender, EventArgs e) { try { var cli ...

  2. k8s 基础 docker-ce 安装(注k8s 的安装需要用此版docker 否则会报错 )

    yum install -y yum-utils yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/ ...

  3. HTTP ERROR

    HTTP 400 – 请求无效HTTP 401.1 – 未授权:登录失败HTTP 401.2 – 未授权:服务器配置问题导致登录失败HTTP 401.3 – ACL 禁止访问资源HTTP 401.4 ...

  4. p4555&bzoj2565 最长双回文串

    传送门(洛谷) 传送门(bzoj) 题目 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为abc,逆序为cba,不相同). 输入长度为 nnn 的串 SSS ...

  5. [转] Hibernate不能自动建表解决办法(hibernate.hbm2ddl.auto) (tables doesn't exist)

    转自: http://blog.csdn.net/biangren/article/details/8010018 最近开始学Hibernate,看的是李刚的那本<轻量级java ee企业应用实 ...

  6. 介绍一款“对话框”组件之 “artDialog”在项目中的使用

    在实际开发项目中经常会用到对话框组件,提示一些信息.其实有很多,例如:在项目中常用到的“Jquery-UI.Jquery-EasyUI”的.Dialog,他们也很强大,Api文档也很多.今天就介绍一款 ...

  7. Linux 最小系统挂载U盘(SD、TF卡)并执行程序

    一.在Ubuntu下编译C文件 使用指令"arm-none-linux-gnueabi-gcc-4.4.1 -o HelloWorld HelloWorld.c -static"编 ...

  8. C++基础之预处理和语句

    (1)C++语言源程序中可以使用一些预处理中的编译命令,这些命令在程序被正常编译之前执行,被称为预处理命令,这些命令所实现的功能被称为预处理功能(2)常用的预处理命令有文件包含命令.宏定义命令和条件编 ...

  9. Glance代码分析

    V2版本的glance-api采用Proxy的方式来构建对象(对象套对象),有点类似装饰器模式,包裹的顺序是 Auth(外层) -> Notifier -> Policy -> Qu ...

  10. Hyperledger Cello 安装遇到问题

    Hyperledger Cello 安装遇到问题  8083 我跟着这篇教程 https://github.com/hyperledger/cello/blob/master/docs/setup_m ...