iPhone開發應用中案例實現舉例

來源:互聯網
上載者:User

iPhone開發應用中案例實現舉例是本文要介紹的內容,主要是來學習以下小案例的實現過程,來看詳細內容。

一、從 iPhone/iPad 圖片庫中讀取圖片的代碼

如果您的App涉及到從iPhone/iPad圖片庫讀取圖片,不妨看看CocoaChina版主“angellixf”分享的代碼,會為您節省很多時間。

 
  1. UIImagePickerController * picker = [[UIImagePickerController alloc] init];  
  2. picker.delegate = self;  
  3. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  4. [self presentModalViewController:picker animated:YES];  
  5.  
  6. UIImagePickerControllerSourceTypePhotoLibrary,// 相片庫  
  7. UIImagePickerControllerSourceTypeCamera//相機擷取圖片  
  8. UIImagePickerControllerSourceTypeSavedPhotosAlbum// 這個是自訂庫,是由使用者或儲存到裡面的 

二、將圖片儲存到相片庫的代碼:

 
  1. UIImageWriteToSavedPhotosAlbum(Image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); 

解析NSString形式xml的代碼

CocoaChina會員“marshluca”提出的問題:

 
  1. NSString *xmlString = @"<person><name>Jack</name><age>13< /age></person>"; 

如 何對這個xmlString構造一個NSXML,以及如何解析構造的NSXML.

解決方案:先轉換成NSData,然後用NSXMlParser進行解析。代碼:

 
  1. - (void)handleXMLData {       
  2.     NSString *myString = @"<addresses owner='swilson'><person><lastName>Doe</lastName><firstName>John</firstName></person></addresses>";   
  3.     NSData *myRequestData = [ NSData dataWithBytes: [myString UTF8String]  length:[myString length]];     
  4.     NSXMLParser *myParser = [[NSXMLParser alloc] initWithData:myRequestData];   
  5.     [myParser setDelegate:self];   
  6.     [myParser setShouldProcessNamespaces:YES];   
  7.     [myParser setShouldReportNamespacePrefixes:YES];   
  8.     [myParser setShouldResolveExternalEntities:NO];   
  9.     BOOL success = [myParser parse];   
  10.     [myParser release]; 

文章地址 http://www.cocoachina.com/bbs/read.php?tid-20278.html

三、在iPhone播放背景音樂和按鍵生效的代碼

1、背景音樂播放    支援mp3格式 迴圈播放長音樂

這種播放音樂的方式匯入架構#import <AVFoundation/AVFoundation.h>;

 
  1. NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:@"changan" ofType:@"mp3"];       //建立音樂檔案路徑  
  2.   NSURL *musicURL = [[NSURL alloc] initFileURLWithPath:musicFilePath];    
  3.  
  4.    AVAudioPlayer *thePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];  
  5.  
  6. // 建立播放器  
  7.   self.myBackMusic = thePlayer;    //賦值給自己定義的類變數  
  8.     
  9.   [musicURL release];  
  10.   [thePlayer release];  
  11.     
  12.   [myBackMusic prepareToPlay];  
  13.   [myBackMusic setVolume:1];   //設定音量大小  
  14.   myBackMusic.numberOfLoops = -1;//設定音樂播放次數  -1為一直迴圈  
  15.   if (mainMusicStatus)  
  16.   {  
  17.    [myBackMusic play];   //播放  
  18.   } 

2、按鈕播放聲音

需要匯入架構#import <AudioToolbox/AudioToolbox.h> 

 
  1. NSString *thesoundFilePath = [[NSBundle mainBundle] pathForResource:@"Clapping Crowd Studio 01" ofType:@"caf"];    //建立音樂檔案路徑  
  2. CFURLRef thesoundURL = (CFURLRef) [NSURL fileURLWithPath:thesoundFilePath];  
  3. AudioServicesCreateSystemSoundID(thesoundURL, &sameViewSoundID);  
  4.  
  5. //變數SoundID與URL對應  
  6.  
  7. AudioServicesPlaySystemSound(sameViewSoundID);  // 播放SoundID聲音 

小結:iPhone開發應用中案例實現舉例的內容介紹完了,希望通過本文的學習能對你有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.