zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    RESTful规范1
    Django -- 发送HTML格式的邮件
    11.10 vue
    Selenium 使用
    Beautiful Soup的用法
    Pthon常用模块之requests,urllib和re
    爬虫--工具安装Jupyter anaconda
    11-3
    Python -- tabulate 模块,
    Python -- queue队列模块
  • 原文地址:https://www.cnblogs.com/0515offer/p/4696186.html
Copyright © 2011-2022 走看看