本文主要是介绍Object-C 中各数据类型转换 NSData NSMutableData NSString Byte UIImage 合并音频,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1,NSData 与 NSString
NSData –> NSString
NSString *aString = [[NSString alloc] initWithData:adata encoding:NSUTF8StringEncoding];
NSString –> NSData
NSString *aString = @"1234";NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncoding];
2,NSData 与 Byte
NSData –> Byte
NSString *testString = @"1234567890";NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];Byte *testByte = (Byte *)[testData bytes];
Byte –> NSData
Byte byte[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};NSData *adata = [[NSData alloc] initWithBytes:byte length:24];
3,NSData 与 UIImage
NSData –> UIImage
UIImage *aimage = [UIImage imageWithData: imageData];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
NSData *imageData = UIImagePNGRepresentation(aimae);
4,NSData 与 NSMutableData
NSData NSData *data=[[NSData alloc]init];[[NSMutableData alloc]initWithData:data];
5,合并音频
NSString *mp3Path1 = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp3"];NSString *mp3Path2 = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"mp3"];NSData *sound1Data = [[NSData alloc] initWithContentsOfFile: mp3Path1];NSData *sound2Data = [[NSData alloc] initWithContentsOfFile: mp3Path2];NSMutableData *sounds = [NSMutableData alloc];[sounds appendData:sound1Data];[sounds appendData:sound2Data];NSLog(@"data length:%d", [sounds length]);[sounds writeToFile:[self filePathWithName:@"tmp.mp3"] atomically:YES];
这篇关于Object-C 中各数据类型转换 NSData NSMutableData NSString Byte UIImage 合并音频的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!