在這里,主要為大家介紹一下,怎樣從相冊選取圖片并在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];
[actionSheetshowInView: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:pickeranimated:YES];
}
-(void)LocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];
picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate =self;
//設置選擇后的圖片可被編輯
picker.allowsEditing =YES;
[selfpresentModalViewController:pickeranimated:YES];
}
//實現圖像選取器控制器的委托
-(void)p_w_picpathPickerController:(UIImagePickerController *)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info
{
//用UIImagePickerController選擇、顯示圖片或視頻
NSString *type = [infoobjectForKey:UIImagePickerControllerMediaType];
//當選擇的類型是圖片
if ([typeisEqualToString:@"public.p_w_picpath"])
{
//先把圖片轉成NSData
UIImage* p_w_picpath = [infoobjectForKey:@"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
[fileManagercreateDirectoryAtPath: documentsPathwithIntermediateDirectories:YESattributes:nilerror:nil];
[fileManagercreateFileAtPath:[ documentsPathstringByAppendingString:@"/p_w_picpath.png"]contents:dataattributes:nil];
//得到選擇后沙盒中圖片的完整路徑
filePath = [[NSStringalloc]initWithFormat:@"%@%@", documentsPath, @"/p_w_picpath.png"];
//關閉相冊界面
[pickerdismissModalViewControllerAnimated: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:soundUrlerror: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)];
[buttonsetTitle:@"back"forState:UIControlStateNormal];
[buttonaddTarget:selfaction:@selector(backItem)forControlEvents:UIControlEventTouchUpInside];
[toolBaraddSubview:button];
[moviePlayer.viewaddSubview:toolBar];
[moviePlayerplay];
[moviePlayerstop];
}
}
-(void)myMovieFinishedCallback:(NSNotification*)notify
{
//視頻播放對象
MPMoviePlayerController* theMovie = [notifyobject];
//銷毀播放通知
[[NSNotificationCenterdefaultCenter]removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[theMovie.viewremoveFromSuperview];
}
文章題目:ios應用程序-圖像瀏覽及音頻播放
標題來源:http://vcdvsql.cn/article48/pdschp.html
成都網站建設公司_創新互聯,為您提供App設計、定制開發、搜索引擎優化、域名注冊、手機網站建設、網站內鏈
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯