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

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

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

一、从 iPhone/iPad 图片库中读取图片的代码

如果您的App涉及到从iPhone/iPad图片库读取图片,不妨看看CocoaChina版主“angellixf”分享的代码,会为您节省很多时间。

UIImagePickerController * picker = [[UIImagePickerController alloc] init];  
picker.delegate = self;  
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
[self presentModalViewController:picker animated:YES];  
 
UIImagePickerControllerSourceTypePhotoLibrary,// 相片库  
UIImagePickerControllerSourceTypeCamera//相机获取图片  
UIImagePickerControllerSourceTypeSavedPhotosAlbum// 这个是自定义库,是由用户截图或保存到里面的 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

二、将图片保存到相片库的代码:

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

解析NSString形式xml的代码

CocoaChina会员“marshluca”提出的问题:

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

如 何对这个xmlString构造一个NSXML,以及如何解析构造的NSXML.

解决方法:先转换成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]; 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

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

三、在iPhone播放背景音乐和按键生效的代码

1、背景音乐播放    支持mp3格式 循环播放长音乐

这种播放音乐的方式导入框架#import <AVFoundation/AVFoundation.h>;

NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:@"changan" ofType:@"mp3"];       //创建音乐文件路径  
  NSURL *musicURL = [[NSURL alloc] initFileURLWithPath:musicFilePath];    
 
   AVAudioPlayer *thePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];  
 
// 创建播放器  
  self.myBackMusic = thePlayer;    //赋值给自己定义的类变量  
    
  [musicURL release];  
  [thePlayer release];  
    
  [myBackMusic prepareToPlay];  
  [myBackMusic setVolume:1];   //设置音量大小  
  myBackMusic.numberOfLoops = -1;//设置音乐播放次数  -1为一直循环  
  if (mainMusicStatus)  
  {  
   [myBackMusic play];   //播放  
  } 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

2、按钮播放声音

需要导入框架#import <AudioToolbox/AudioToolbox.h> 

NSString *thesoundFilePath = [[NSBundle mainBundle] pathForResource:@"Clapping Crowd Studio 01" ofType:@"caf"];    //创建音乐文件路径  
CFURLRef thesoundURL = (CFURLRef) [NSURL fileURLWithPath:thesoundFilePath];  
AudioServicesCreateSystemSoundID(thesoundURL, &sameViewSoundID);  
 
//变量SoundID与URL对应  
 
AudioServicesPlaySystemSound(sameViewSoundID);  // 播放SoundID声音 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

小结:iPhone开发应用中案例实现举例的内容介绍完了,希望通过本文的学习能对你有所帮助!

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

2011-08-15 15:44:46

iPhone开发PDF

2011-08-16 15:48:37

iPhone开发抓图程序

2011-08-19 11:10:31

iPhone应用

2011-08-19 10:13:05

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 18:02:32

iPhone开发表视图

2011-08-15 11:23:41

iPhone开发循环滚动UIScrollVie

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-16 15:36:47

iPhone应用测试

2011-08-17 16:23:31

iPhone开发UIViewContr

2011-08-22 14:12:48

iPhone开发NSTableView

2011-08-15 11:37:20

iPhone开发Mask

2011-08-12 14:33:06

iPhone缓存文件

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-08-19 10:01:09

iPhone应用SqliteUITableView

2012-04-26 13:26:58

iPhone应用技巧

2011-08-05 13:49:53

iPhone 应用 开发
点赞
收藏

51CTO技术栈公众号