Example of case implementation in iPhone development applications

Source: Internet
Author: User

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.

 
 
  1. UIImagePickerController * picker = [[UIImagePickerController alloc] init];
  2. Picker. delegate = self;
  3. Picker. sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  4. [Self presentModalViewController: picker animated: YES];
  5.  
  6. UIImagePickerControllerSourceTypePhotoLibrary, // photo library
  7. UIImagePickerControllerSourceTypeCamera // obtain the image from the camera
  8. UIImagePickerControllerSourceTypeSavedPhotosAlbum // This is a custom library, which is saved by the user or

Ii. Code for saving images to the Photo Library:

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

Parse NSString xml Code

Questions raised by the CocoaChina member "marshluca:

 
 
  1. 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:

 
 
  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]; 

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>;

 
 
  1. NSString * musicFilePath = [[NSBundle mainBundle] pathForResource: @ "changan" ofType: @ "mp3"]; // create a music file path
  2. NSURL * musicURL = [[NSURL alloc] initFileURLWithPath: musicFilePath];
  3.  
  4. AVAudioPlayer * thePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: musicURL error: nil];
  5.  
  6. // Create a player
  7. Self. myBackMusic = thePlayer; // assign a value to the class variable defined by yourself
  8. [MusicURL release];
  9. [ThePlayer release];
  10. [MyBackMusic prepareToPlay];
  11. [MyBackMusic setVolume: 1]; // sets the volume.
  12. MyBackMusic. numberOfLoops =-1; // set the number of times the music is played-1 to keep repeating
  13. If (mainMusicStatus)
  14. {
  15. [MyBackMusic play]; // playback
  16. }

2. Button playing sound

You need to import the framework # import <AudioToolbox/AudioToolbox. h>

 
 
  1. NSString * thesoundFilePath = [[NSBundle mainBundle] pathForResource: @ "Clapping Crowd Studio 01" ofType: @ "caf"]; // create a music file path
  2. CFURLRef thesoundURL = (CFURLRef) [NSURL fileURLWithPath: thesoundFilePath];
  3. AudioServicesCreateSystemSoundID (thesoundURL, & sameViewSoundID );
  4.  
  5. // The SoundID variable corresponds to the URL
  6.  
  7. 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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.