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