iPhone第五节:多媒体和手势

本文介绍了一款包含多媒体播放、手势演示与工作演示功能的应用,通过按钮触发不同操作,包括相机拍照、相册选择、音视频播放、手势演示与工作演示,实现多媒体与手势的无缝集成。

多媒体和手势



#import <UIKit/UIKit.h>

@interface CameraViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate>

@property (nonatomic, strong) UIImageView * cameraImageView;

@end


#import "CameraViewController.h"
#import "MovieViewController.h"
#import "GestureViewController.h"
#import "WorkViewController.h"

@interface CameraViewController ()

@end

@implementation CameraViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //背景
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImage * image = [UIImage imageNamed:@"壁纸1.jpg"];
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    //初始化
    [self initCameraBtnAndCameraImageView];
    
#pragma mark ==Button
    UIButton * btnLogin1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnLogin1.frame = CGRectMake(45, 415, 230, 45);
    btnLogin1.showsTouchWhenHighlighted = YES;
    [btnLogin1 setTitle:@"音视频播放" forState:UIControlStateNormal];
    [btnLogin1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnLogin1 addTarget:self action:@selector(movie) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnLogin1];
    
#pragma mark ==Button
    UIButton * btnLogin2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnLogin2.frame = CGRectMake(45, 460, 230, 45);
    btnLogin2.showsTouchWhenHighlighted = YES;
    [btnLogin2 setTitle:@"手势演示" forState:UIControlStateNormal];
    [btnLogin2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnLogin2 addTarget:self action:@selector(gesture) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnLogin2];
    
#pragma mark ==Button
    UIButton * btnLogin3 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnLogin3.frame = CGRectMake(45, 505, 230, 45);
    btnLogin3.showsTouchWhenHighlighted = YES;
    [btnLogin3 setTitle:@"Work演示" forState:UIControlStateNormal];
    [btnLogin3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnLogin3 addTarget:self action:@selector(work) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnLogin3];
}

//初始化
- (void)initCameraBtnAndCameraImageView
{
    //新建btn,添加事件
    UIButton * cameraBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cameraBtn.frame = CGRectMake(120, 150, 80, 120);
    [cameraBtn addTarget:self action:@selector(showActionSheet) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cameraBtn];
    
    //添加图片视图
    _cameraImageView = [[UIImageView alloc] initWithFrame:CGRectMake(120, 150, 80, 120)];
    _cameraImageView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_cameraImageView];
}

- (void)showActionSheet
{
    //下方推出菜单
    UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"相册", nil];
    [actionSheet showInView:self.view];
}

//设置点击菜单的响应
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        [self addCamera];
    }
    else if (buttonIndex == 1)
    {
        [self addPhoto];
    }
}

//打开相机
- (void)addCamera
{
    //判断是否可以打开相机,模拟器此功能无法使用
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        //创建一个调出拍照的控制器
        UIImagePickerController * picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //可以选取某一部分
        picker.allowsEditing = YES;
        //资源类型,摄像头(相册、保存)
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:picker animated:YES completion:nil];
    }
    else
    {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无法启用相机" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}

- (void)addPhoto
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController * picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:nil];
    }
    else
    {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无法启用" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}

//选择结束调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //info传的字典,根据键得到值,给image赋值(编辑过的,源图)
    UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
    //图片存入相册
    //UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    _cameraImageView.image = image;
    [self dismissViewControllerAnimated:YES completion:nil];
}

//点击取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)movie
{
    MovieViewController * movieVC = [[MovieViewController alloc] init];
    movieVC.modalTransitionStyle = 1;
    [self presentViewController:movieVC animated:YES completion:nil];
}

- (void)gesture
{
    GestureViewController * gestureVC  = [[GestureViewController alloc] init];
    gestureVC.modalTransitionStyle = 1;
    [self presentViewController:gestureVC animated:YES completion:nil];
}

- (void)work
{
    WorkViewController * workVC  = [[WorkViewController alloc] init];
    workVC.modalTransitionStyle = 1;
    [self presentViewController:workVC animated:YES completion:nil];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

#import <UIKit/UIKit.h>

@interface MovieViewController : UIViewController

@end

#import "MovieViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>

@interface MovieViewController ()

@property(nonatomic, strong) AVAudioPlayer *player;

@end

@implementation MovieViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
#pragma mark ==Button
    UIButton * btnLogin1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnLogin1.frame = CGRectMake(45, 100, 230, 45);
    btnLogin1.showsTouchWhenHighlighted = YES;
    [btnLogin1 setTitle:@"播放视频" forState:UIControlStateNormal];
    [btnLogin1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnLogin1 addTarget:self action:@selector(moviePlay) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnLogin1];
    
#pragma mark ==Button
    UIButton * btnLogin2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btnLogin2.frame = CGRectMake(45, 200, 230, 45);
    btnLogin2.showsTouchWhenHighlighted = YES;
    [btnLogin2 setTitle:@"播放音频" forState:UIControlStateNormal];
    [btnLogin2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnLogin2 addTarget:self action:@selector(voicePlay) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnLogin2];
}

- (void)moviePlay
{
    //本地视频
//    NSString * path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"m4v"];
//    NSURL * url = [[NSURL alloc] initFileURLWithPath:path];
//    MPMoviePlayerViewController * player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
//    //弹出视图
//    //[self presentMoviePlayerViewControllerAnimated:player];
//    //[self presentViewController:player animated:YES completion:nil];  //也可以
//    
//    //子视图
//    [player.view setFrame:CGRectMake(0, 100, 320, 400)];
//    [self.view addSubview:player.view];
//    //[player play];  //MPMoviePlayerController  才可以用play
    
    //网络视频
//    NSURL * url = [NSURL URLWithString:@"http://123.57.75.80:8001/data//news/video//2015/06/20150609134637_60491.mp4"];
//    if (url)
//    {
//        MPMoviePlayerViewController * player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
//        //资源类型
////        MPMovieSourceTypeUnknown,
////        MPMovieSourceTypeFile,     // Local or progressively downloaded network content
////        MPMovieSourceTypeStreaming // Live or on-demand streaming content
//        player.moviePlayer.movieSourceType = MPMovieSourceTypeUnknown;
//        //控制风格
////        MPMovieControlStyleNone,       // No controls
////        MPMovieControlStyleEmbedded,   // Controls for an embedded view,默认,嵌入
////        MPMovieControlStyleFullscreen, // Controls for fullscreen playback
//        player.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
//        //设置全屏
//        player.moviePlayer.fullscreen = YES;
//        //缩放模式
////        MPMovieScalingModeNone,       // No scaling
////        MPMovieScalingModeAspectFit,  // Uniform scale until one dimension fits
////        MPMovieScalingModeAspectFill, // Uniform scale until the movie fills the visible bounds. One dimension may have clipped contents
////        MPMovieScalingModeFill        // Non-uniform scale. Both render dimensions will exactly match the visible bounds
//        player.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
////            UIModalTransitionStyleCoverVertical = 0,
////            UIModalTransitionStyleFlipHorizontal,
////            UIModalTransitionStyleCrossDissolve,
////#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2
////            UIModalTransitionStylePartialCurl,
////#endif
//        player.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
//        [self presentMoviePlayerViewControllerAnimated:player];
//    }
//    if (!url)
//    {
//        NSLog(@"ERROR");
//    }
    
    
    //网络视频
    NSURL *url = [NSURL URLWithString:@"http://123.57.75.80:8001/data//news/video//2015/06/20150609134637_60491.mp4"];
    MPMoviePlayerViewController *playVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    playVC.view.frame = CGRectMake(0, 100, 320, 200);
    [playVC.moviePlayer setControlStyle:MPMovieControlStyleEmbedded];
    playVC.moviePlayer.scalingMode = MPMovieScalingModeFill;
    [self.view addSubview:playVC.view];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeFull) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
    //[self presentMoviePlayerViewControllerAnimated:playVC];
}

- (void)voicePlay
{
//    //本地音频
//    NSString * path = [[NSBundle mainBundle] pathForResource:@"一江水" ofType:@"mp3"];
//    if (path)
//    {
//        NSURL * sUrl = [[NSURL alloc] initFileURLWithPath:path];
//        AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:sUrl error:nil];
//        [player prepareToPlay];
//        player.numberOfLoops = 0;
//        player.volume = 1.0f;
//        if (player)
//        {
//            if (![player isPlaying])
//            {
//                [player play];
//            }
//        }
//    }

    
    //网络音频
    NSURL *url = [NSURL URLWithString:@""];
    
    MPMoviePlayerViewController *playVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    playVC.view.frame = CGRectMake(0, 100, 320, 200);
    [playVC.moviePlayer setControlStyle:MPMovieControlStyleEmbedded];
    playVC.moviePlayer.scalingMode = MPMovieScalingModeFill;
    [self.view addSubview:playVC.view];

}

-(void)changeFull
{
    NSLog(@"++++++++++");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


#import <UIKit/UIKit.h>

@interface GestureViewController : UIViewController

@property (nonatomic, retain) IBOutlet UIImageView * imageView;

@end

#import "GestureViewController.h"

@interface GestureViewController ()

@end

@implementation GestureViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //点击一次
    UITapGestureRecognizer * tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addOne)];
    [tapOnce setNumberOfTapsRequired:1];  //点击次数
    tapOnce.numberOfTouchesRequired = 1;  //手指数量
    [self.view addGestureRecognizer:tapOnce];
    
    //点击两次
    UITapGestureRecognizer * tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addTwo)];
    [tapTwice setNumberOfTapsRequired:2];
    tapTwice.numberOfTouchesRequired = 1;
    [self.view addGestureRecognizer:tapTwice];
    //如果不加下面的话,当单指双击时,会先调用单指单击中的处理,再调用单指双击中的处理
    [tapOnce requireGestureRecognizerToFail:tapTwice];
    
    //长按
    UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(addThree:)];
    [self.view addGestureRecognizer:longPress];
    
    //长按(无效)
    UILongPressGestureRecognizer * longPressImage = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(addSeven)];
    _imageView.userInteractionEnabled = YES;
    [_imageView addGestureRecognizer:longPressImage];
    
    //上滑
    UISwipeGestureRecognizer * swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(addFour)];
    //滑动方向
    swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    [self.view addGestureRecognizer:swipeUp];
    
    //下滑
    UISwipeGestureRecognizer * swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(addFive)];
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:swipeDown];
    
    //旋转
    UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(addSix)];
    [self.view addGestureRecognizer:rotation];
    
    //捏合
    UIPinchGestureRecognizer * pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(addSeven)];
    [self.view addGestureRecognizer:pin];
}

- (void)addOne
{
    self.imageView.image = [UIImage imageNamed:@"1.jpg"];
}

- (void)addTwo
{
    self.imageView.image = [UIImage imageNamed:@"2.jpg"];
}

- (void)addThree:(UILongPressGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateBegan)
        _imageView.image = [UIImage imageNamed:@"3.jpg"];
}

- (void)addFour
{
    _imageView.image = [UIImage imageNamed:@"4.jpg"];
}

- (void)addFive
{
    _imageView.image = [UIImage imageNamed:@"5.jpg"];
}

- (void)addSix
{
    _imageView.image = [UIImage imageNamed:@"6.jpg"];
}

- (void)addSeven
{
    _imageView.image = [UIImage imageNamed:@"7.jpg"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化并行计算等改进策略。; 适合人群:具备一定Python编程基础优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值