您好,登錄后才能下訂單哦!
在這里,主要為大家介紹一下,怎樣從相冊選取圖片并在ImageView中顯示出你選擇的圖片,并且實現簡單的音樂和視頻播放,下面是運行時的主頁面效果圖:
下面我們仔細學習具體的細節。創建一個空的IOS項目,接著在創建一個ViewController。
AppDelegate.h 應用的代理類這個沒什么好說的就是直接打開剛剛創建的新ViewController,在ViewController.h文件里添加如下代碼:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
//注意這里面引入了很多代理類
@interface ViewController: UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,AVAudioPlayerDelegate>
一.圖片的選取
點擊導航欄的photo按鈕會跳轉到photoView頁面,至于如何跳轉,下面介紹一種簡單的跳轉方式,則是在storyboard中右鍵點中photo拖拉到你要跳轉的頁面,在storyboard segues中有3個選項,Push,Modal和Custom,選中Modal,再次運行,點擊photo頁面跳轉成功。
這時點擊導航欄上的camera,會在下方彈出一個UIActionSheet,選擇從手機相冊獲取之后回呈現相冊里的圖片,根據需求選擇完之后會在ImageView中顯示出來相應的圖片,具體效果圖如下:
在項目中添加類文件photoViewController系統自動生成photoViewController.h (頭文件)和photoViewController.m(實現文件),在photoViewController.m中從相冊選取圖片的主要程序如下:
- (IBAction)btnPressed:(id)sender {
UIActionSheet*actionSheet = [[UIActionSheetalloc]
initWithTitle:nil
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles: @"打開照相機", @"從手機相冊獲取",nil];
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheetclickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == actionSheet.cancelButtonIndex)
{
NSLog(@"取消");
}
switch (buttonIndex)
{
case 0:
//打開照相機拍照
[selftakePhoto];
break;
case 1:
//打開本地相冊
[selfLocalPhoto];
break;
}
}
-(void)takePhoto
{
UIImagePickerController *picker=[[UIImagePickerControlleralloc]init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate=self;
picker.allowsEditing=YES;
[selfpresentModalViewController:picker animated:YES];
}
-(void)LocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerControlleralloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
//設置選擇后的圖片可被編輯
picker.allowsEditing = YES;
[selfpresentModalViewController:picker animated:YES];
}
//實現圖像選取器控制器的委托
-(void)p_w_picpathPickerController:(UIImagePickerController *)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info
{
//用UIImagePickerController選擇、顯示圖片或視頻
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//當選擇的類型是圖片
if ([type isEqualToString:@"public.p_w_picpath"])
{
//先把圖片轉成NSData
UIImage* p_w_picpath = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data;
//判斷圖片是不是JPEG格式的文件
if (UIImagePNGRepresentation(p_w_picpath))
{
data = UIImageJPEGRepresentation(p_w_picpath, 1.0);
}
else
{
data = UIImagePNGRepresentation(p_w_picpath);
}
//圖片保存的路徑
//指定文件目錄這里將圖片放在沙盒的documents文件夾中
NSString * documentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager *fileManager = [NSFileManagerdefaultManager];
//把剛剛圖片轉換的data對象拷貝至沙盒中并保存為p_w_picpath.png
[fileManager createDirectoryAtPath: documentsPath withIntermediateDirectories:YESattributes:nilerror:nil];
[fileManager createFileAtPath:[ documentsPath stringByAppendingString:@"/p_w_picpath.png"] contents:data attributes:nil];
//得到選擇后沙盒中圖片的完整路徑
filePath = [[NSStringalloc]initWithFormat:@"%@%@", documentsPath, @"/p_w_picpath.png"];
//關閉相冊界面
[picker dismissModalViewControllerAnimated:YES];
p_w_picpathView.p_w_picpath = p_w_picpath;
//加在視圖中
[self.viewaddSubview:p_w_picpathView];
}
}
二.音樂的播放:
音樂由頁面中的PickVideo按鈕觸發播放音頻文件,iPhone開發中想要實現音頻的播放是很容易的,AVFoundation框架就是Apple本身推薦使用的一種方式。如何引入這個框架,下面為大家詳細介紹:
首先,點擊工程文件夾,會出現左邊的界面,選擇我圈起來的那個加號,會出現一系列的框架給你選擇,你只需要選擇AVFoundation.framework即可。此時,在它的上方就會顯示出來,這個框架就添加好了。
播放音樂所需要的程序如下:
- (IBAction)playMp4File:(id)sender {
// //找到mp3在資源庫中的路徑文件名稱為sound 類型為mp3
NSString *soundPath=[[NSBundlemainBundle] pathForResource:@"后來"ofType:@"mp3"];
if(soundPath)
{
NSURL *soundUrl=[[NSURLalloc] initFileURLWithPath:soundPath];
player=[[AVAudioPlayeralloc] initWithContentsOfURL:soundUrl error:nil];
//初始化播放器
[playerprepareToPlay];
//設置播放循環次數,如果numberOfLoops為負數音頻文件就會一直循環播放下去
player.numberOfLoops = -1;
//設置音頻音量 volume的取值范圍在 0.0為最小 0.1為最大可以根據自己的情況而設置
player.volume = 0.5f;
}
//當player有值的情況下并且沒有在播放中開始播放音樂
if (player)
{
if (![playerisPlaying])
{
[playerplay];
}
}
}
- (IBAction)palyStop:(id)sender {
//停止播放聲音
if (player) {
if ([playerisPlaying]) {
[playerstop];
}
}
}
三.視頻的播放:
視頻的播放由頁面中的play MP4 File ,play Stop觸發,而且播放電影文件時需要注意:ios中可以使用MPMoviePlayerController來播放電影文件這個類定義在MediaPlayer.framework中。同理,添加MediaPlayer.framework框架。
下面是觸發pivkVideo時的代碼:
- (IBAction)pickVideo:(id)sender
{
NSString *videoPath=[[NSBundlemainBundle] pathForResource:@"犯罪高手" ofType:@"mp4"];
NSLog(@"%@",videoPath);
if(videoPath)
{
NSURL *videoUrl=[[NSURLalloc] initFileURLWithPath:videoPath];
//視頻播放對象
moviePlayer = [[MPMoviePlayerControlleralloc]
initWithContentURL:videoUrl];
//適應屏幕大小,保持寬高比
moviePlayer.controlStyle=MPMovieScalingModeAspectFit;
[moviePlayer.viewsetFrame:self.view.bounds];
moviePlayer.initialPlaybackTime = -1;
//顯示播放/暫停、音量和時間控制
moviePlayer.movieControlMode = MPMovieControlModeDefault;
[self.viewaddSubview:moviePlayer.view];
// 注冊一個播放結束的通知
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
UIToolbar *toolBar=[[UIToolbaralloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIButton *button=[[UIButtonalloc] initWithFrame:CGRectMake(20, 3, 40, 40)];
[button setTitle:@"back"forState:UIControlStateNormal];
[button addTarget:selfaction:@selector(backItem) forControlEvents:UIControlEventTouchUpInside];
[toolBar addSubview:button];
[moviePlayer.viewaddSubview:toolBar];
[moviePlayerplay];
[moviePlayerstop];
}
}
-(void)myMovieFinishedCallback:(NSNotification*)notify
{
//視頻播放對象
MPMoviePlayerController* theMovie = [notify object];
//銷毀播放通知
[[NSNotificationCenterdefaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[theMovie.viewremoveFromSuperview];
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。