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 ...
随机推荐
- Sublime Text3常用插件以及安装方法(实用)【转载】
https://www.cnblogs.com/liuchaoH/p/6370008.html Package Control组件在线安装 按Ctrl+`调出console(注:避免热键冲突) 粘贴以 ...
- 关于RTC的浅学
最近公司业务主要是移动客户端,所以免不了客户端与服务端之间的通信.第一次接触通信,做点基本概念的笔记. 主要架构是:openfire+xmpp+play+移动客户端,下文理下这几个概念. OpenFi ...
- OpenGL — GLFW — 颜色
OpenGL - GLFW - 颜色 参考教程:https://learnopengl-cn.readthedocs.io/zh/latest/02%20Lighting/01%20Colors/ 既 ...
- 28.【转载】挖洞技巧:APP手势密码绕过思路总结
说到APP手势密码绕过的问题,大家可能有些从来没接触过,或者接触过,但是思路也就停留在那几个点上,这里我总结了我这1年来白帽子生涯当中所挖掘的关于这方面的思路,有些是网上已经有的,有些是我自己不断摸索 ...
- “MVC+Nhibernate+Jquery-EasyUI” 信息发布系统 第五篇(用户管理之“用户权限分配”)
一.在做权限分配之前,首先先了解“ZTree”这个插件,我的这个系统没有用Jquery-EasyUI的Tree.用的是”ZTree“朋友们可以试试,也很强大.点击下载ZTree插件. 1. ...
- Algorithms - Fibonacci Number
斐波那契数列(Fibonacci Number)从数学的角度是以递归的方法定义的: \(F_0 = 0\) \(F_1 = 1\) \(F_n = F_{n-1} + F_{n-2}\) (\(n \ ...
- oracle数据库rownum讲解(转)
https://blog.csdn.net/qq_40794266/article/details/78698321
- java实例练习——基于TCP/IP协议的多客户端通信
先说一下大概的思路: 应用多线程来实现服务器与多客户端之间的通信 1.服务器端创建ServerSocket,循环调用accept()等待客户端连接: 2.客户端创建一个Socket并请求与服务器端连接 ...
- 初等变换求 |A| % Mod & A- % Mod & A* % Mod(模板)
// |A| * A- = A* (伴随矩阵) = 逆矩阵 * 矩阵的值 #include<cstdio> #include<cstring> #include<cstd ...
- Java基础笔记(七)—— 成员变量、静态变量、局部变量
public class Test { int c; //成员变量(实例变量) static int s1; //静态变量(类变量)(全局变量) public static void main(Str ...