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

1原文摘自:http://mobile.51cto.com/iphone-284840.htm

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

 

posted on 2011-12-06 22:09  wtq  阅读(265)  评论(0编辑  收藏  举报