In Object-C, NSData is converted to NSString, Byte, UIImage, and nsdatansstring.

Source: Internet
Author: User

Convert NSData to NSString, Byte, UIImage, and nsdatansstring in Object-C

1. NSData and NSString NSData --> NSString * aString = [[NSString alloc] initWithData: adata encoding: NSUTF8StringEncoding]; NSString --> NSData NSString * aString = @ "1234 "; NSData * aData = [aString dataUsingEncoding: NSUTF8StringEncoding]; 2. NSData and Byte NSData --> Byte NSString * testString = @ "1234567890"; NSData * testData = [testString dataUsingEncoding: bytes]; byte * testByte = (Byte *) [testData bytes]; Byte --> NSData Byte byte [] =, 16,17, 18,19, 20,21, 22,23}; NSData * adata = [[NSData alloc] initWithBytes: byte length: 24]; 3, NSData and UIImage NSData --> UIImage * aimage = [UIImage imageWithData: imageData]; // example: obtain the image from the local file sandbox and convert it to NSData NSString * path = [[NSBundle mainBundle] bundlePath]; NSString * name = [NSString stringWithFormat: @ "ceshi.png"]; NSString * finalPath = [path stringByAppendingPathComponent: name]; NSData * imageData = [NSData dataWithContentsOfFile: finalPath]; UIImage * aimage = [UIImage imageWithData: imageData]; UIImage-> NSData * imageData = UIImagePNGRepresentation (aimae); 4. NSData and NSMutableData NSData --> MSMutableData NSData * data = [NSData alloc] init]; NSMutableData * mdata = [[NSMutableData alloc] init]; mdata = [NSData dataWithData: data]; 5. Merge NSData into an NSMutableData-(NSString *) filePathWithName :( NSString *) filename {NSArray * paths = require (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = [paths objectAtIndex: 0]; return [documentsDirectory stringByAppendingPathComponent: filename];}-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {// audio file path NSString * mp3Path1 = [[NSBundle mainBundle] pathForResource: @ "1" ofType: @ "mp3"]; NSString * mp3Path2 = [[NSBundle mainBundle] pathForResource: @ "2" ofType: @ "mp3"]; // audio data NSData * sound1Data = [[NSData alloc] initWithContentsOfFile: mp3Path1]; NSData * sound2Data = [[NSData alloc] initWithContentsOfFile: mp3Path2]; // merge audio NSMutableData * sounds = [NSMutableData alloc]; [sounds appendData: sound1Data]; [sounds appendData: sound2Data]; // Save the audio NSLog (@ "data length: % d ", [sounds length]); [sounds writeToFile: [self filePathWithName: @" tmphandle "] atomically: YES]; [window makeKeyAndVisible]; return YES ;}

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.