IPhone DevelopmentThe example of case implementation in the application is the content to be introduced in this article, mainly to learn the implementation process of the following small cases, to see the details.
I. Read the image code from the iPhone/iPad Image Library
If your App involves reading images from the iPhone/iPad image library, you can read the code shared by the CocoaChina moderator angellixf, which saves you a lot of time.
- UIImagePickerController * picker = [[UIImagePickerController alloc] init];
- Picker. delegate = self;
- Picker. sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- [Self presentModalViewController: picker animated: YES];
-
- UIImagePickerControllerSourceTypePhotoLibrary, // photo library
- UIImagePickerControllerSourceTypeCamera // obtain the image from the camera
- UIImagePickerControllerSourceTypeSavedPhotosAlbum // This is a custom library, which is saved by the user or
Ii. Code for saving images to the Photo Library:
- UIImageWriteToSavedPhotosAlbum(Image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
Parse NSString xml Code
Questions raised by the CocoaChina member "marshluca:
- NSString *xmlString = @"<person><name>Jack</name><age>13< /age></person>";
How to construct an NSXML for this xmlString and parse the constructed NSXML.
Solution: first convert to NSData and then use NSXMlParser for parsing. Code:
- - (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];
Post address http://www.cocoachina.com/bbs/read.php? Tid-20278.html
3. Code for playing background music and buttons on iPhone
1. Background MusicPlaySupports loop playback of long music in mp3 format
This method of playing music is imported into the framework # import <AVFoundation/AVFoundation. h>;
- NSString * musicFilePath = [[NSBundle mainBundle] pathForResource: @ "changan" ofType: @ "mp3"]; // create a music file path
- NSURL * musicURL = [[NSURL alloc] initFileURLWithPath: musicFilePath];
-
- AVAudioPlayer * thePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: musicURL error: nil];
-
- // Create a player
- Self. myBackMusic = thePlayer; // assign a value to the class variable defined by yourself
- [MusicURL release];
- [ThePlayer release];
- [MyBackMusic prepareToPlay];
- [MyBackMusic setVolume: 1]; // sets the volume.
- MyBackMusic. numberOfLoops =-1; // set the number of times the music is played-1 to keep repeating
- If (mainMusicStatus)
- {
- [MyBackMusic play]; // playback
- }
2. Button playing sound
You need to import the framework # import <AudioToolbox/AudioToolbox. h>
- NSString * thesoundFilePath = [[NSBundle mainBundle] pathForResource: @ "Clapping Crowd Studio 01" ofType: @ "caf"]; // create a music file path
- CFURLRef thesoundURL = (CFURLRef) [NSURL fileURLWithPath: thesoundFilePath];
- AudioServicesCreateSystemSoundID (thesoundURL, & sameViewSoundID );
-
- // The SoundID variable corresponds to the URL
-
- AudioServicesPlaySystemSound (sameViewSoundID); // play the SoundID sound
Summary:IPhone DevelopmentThe example of case implementation in the application has been introduced. I hope this article will help you!