iPhone開發(fā)應(yīng)用中案例實現(xiàn)舉例
iPhone開發(fā)應(yīng)用中案例實現(xiàn)舉例是本文要介紹的內(nèi)容,主要是來學(xué)習(xí)以下小案例的實現(xiàn)過程,來看詳細內(nèi)容。
一、從 iPhone/iPad 圖片庫中讀取圖片的代碼
如果您的App涉及到從iPhone/iPad圖片庫讀取圖片,不妨看看CocoaChina版主“angellixf”分享的代碼,會為您節(jié)省很多時間。
- UIImagePickerController * picker = [[UIImagePickerController alloc] init];
- picker.delegate = self;
- picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- [self presentModalViewController:picker animated:YES];
- UIImagePickerControllerSourceTypePhotoLibrary,// 相片庫
- UIImagePickerControllerSourceTypeCamera//相機獲取圖片
- UIImagePickerControllerSourceTypeSavedPhotosAlbum// 這個是自定義庫,是由用戶截圖或保存到里面的
二、將圖片保存到相片庫的代碼:
- UIImageWriteToSavedPhotosAlbum(Image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
解析NSString形式xml的代碼
CocoaChina會員“marshluca”提出的問題:
- NSString *xmlString = @"<person><name>Jack</name><age>13< /age></person>";
如 何對這個xmlString構(gòu)造一個NSXML,以及如何解析構(gòu)造的NSXML.
解決方法:先轉(zhuǎn)換成NSData,然后用NSXMlParser進行解析。代碼:
- - (void)handleXMLData {
- NSString *myString = @"<addresses owner='swilson'><person><lastName>Doe</lastName><firstName>John</firstName></person></addresses>";
- NSData *myRequestData = [ NSData dataWithBytes: [myString UTF8String] length:[myString length]];
- NSXMLParser *myParser = [[NSXMLParser alloc] initWithData:myRequestData];
- [myParser setDelegate:self];
- [myParser setShouldProcessNamespaces:YES];
- [myParser setShouldReportNamespacePrefixes:YES];
- [myParser setShouldResolveExternalEntities:NO];
- BOOL success = [myParser parse];
- [myParser release];
帖子地址 http://www.cocoachina.com/bbs/read.php?tid-20278.html
三、在iPhone播放背景音樂和按鍵生效的代碼
1、背景音樂播放 支持mp3格式 循環(huán)播放長音樂
這種播放音樂的方式導(dǎo)入框架#import <AVFoundation/AVFoundation.h>;
- NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:@"changan" ofType:@"mp3"]; //創(chuàng)建音樂文件路徑
- NSURL *musicURL = [[NSURL alloc] initFileURLWithPath:musicFilePath];
- AVAudioPlayer *thePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
- // 創(chuàng)建播放器
- self.myBackMusic = thePlayer; //賦值給自己定義的類變量
- [musicURL release];
- [thePlayer release];
- [myBackMusic prepareToPlay];
- [myBackMusic setVolume:1]; //設(shè)置音量大小
- myBackMusic.numberOfLoops = -1;//設(shè)置音樂播放次數(shù) -1為一直循環(huán)
- if (mainMusicStatus)
- {
- [myBackMusic play]; //播放
- }
2、按鈕播放聲音
需要導(dǎo)入框架#import <AudioToolbox/AudioToolbox.h>
- NSString *thesoundFilePath = [[NSBundle mainBundle] pathForResource:@"Clapping Crowd Studio 01" ofType:@"caf"]; //創(chuàng)建音樂文件路徑
- CFURLRef thesoundURL = (CFURLRef) [NSURL fileURLWithPath:thesoundFilePath];
- AudioServicesCreateSystemSoundID(thesoundURL, &sameViewSoundID);
- //變量SoundID與URL對應(yīng)
- AudioServicesPlaySystemSound(sameViewSoundID); // 播放SoundID聲音
小結(jié):iPhone開發(fā)應(yīng)用中案例實現(xiàn)舉例的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!