iPhone开发应用中案例实现举例

移动开发 iOS
iPhone开发应用中案例实现举例是本文要介绍的内容,主要是来学习以下小案例的实现过程,来看详细内容。

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开发应用中案例实现举例的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: starming社区
相关推荐

2011-08-15 15:44:46

iPhone开发PDF

2011-08-16 15:48:37

iPhone开发抓图程序

2011-08-19 10:13:05

iPhone开发

2011-08-19 11:10:31

iPhone应用

2011-08-18 15:24:40

iPhone国际化

2011-08-19 10:05:30

iPhone开发

2011-08-17 16:12:20

iPhone应用程序

2011-08-18 16:42:07

iPhone应用APNS推送

2011-08-15 11:23:41

iPhone开发循环滚动UIScrollVie

2011-08-15 18:02:32

iPhone开发表视图

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-16 15:36:47

iPhone应用测试

2011-08-17 16:23:31

iPhone开发UIViewContr

2011-08-12 14:33:06

iPhone缓存文件

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-08-22 14:12:48

iPhone开发NSTableView

2011-08-15 11:37:20

iPhone开发Mask

2011-08-19 10:01:09

iPhone应用SqliteUITableView

2011-07-25 14:44:41

iPhone iPhone开发 截屏

2011-07-26 14:18:20

点赞
收藏

51CTO技术栈公众号