Object-C 中各数据类型转换 NSData转NSString,Byte,UIImage

转载 2016年06月02日 10:48:54

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];
  //例:从本地文件沙盒中取图片并转换为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
  NSData *imageData = UIImagePNGRepresentation(aimae);


4,NSData 与 NSMutableData
  NSData --> MSMutableData
  NSData *data=[[NSData alloc]init];
  NSMutableData *mdata=[[NSMutableData alloc]init];   
  mdata=[NSData dataWithData:data];


5,NSData合并为一个NSMutableData

- (NSString *)filePathWithName:(NSString *)filename
 {
         NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
         NSString *documentsDirectory = [paths objectAtIndex:0];
         return [documentsDirectory stringByAppendingPathComponent:filename];
 }
 
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
     //音频文件路径
         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];
         
         [window makeKeyAndVisible];
     
     return YES;
 }


文章来源:http://blog.csdn.net/hong1595/article/details/17404477

NSdata 与 NSString,Byte数组,UIImage 的相互转换---ios开发 Objective-C

1. NSData 与 NSString NSData-> NSString NSString *aString = [[NSString alloc] initWithData:adata enco...
  • tao0001
  • tao0001
  • 2012年09月18日 16:46
  • 516

NSdata与NSString,Byte数组,UIImage的相互转换

原址:http://w11h22j33.iteye.com/blog/1333521 1. NSData 与 NSString NSData-> NSString NSStrin...

NSData 与 NSString,Byte数组,UIImage 的相互转换

1. NSData 与 NSString NSData-> NSString NSString *aString = [[NSString alloc] initWithData:adat...

NSData 与 NSString,Byte数组,UIImage 的相互转换

1. NSData 与 NSString NSData-> NSString NSString *aString = [[NSString alloc] initWithData:adatae...

NSData 与 NSString,Byte数组,UIImage 的相互转换

1. NSData 与 NSString NSData-> NSString NSString *aString = [[NSString alloc] initWithData:adataenc...

NSdata 与 NSString,Byte数组,UIImage 的相互转换

原文来自:http://www.devdiv.com/NSdata_与_NSString,Byte数组,UIImage_的相互转换-weblog-66-7483.html 闲来无事,不定期整理点...

NSdata 与 NSString,Byte数组,UIImage 的相互转换

//1. NSData 与 NSString //NSData-> NSString NSString *aString = [[NSString alloc] initWithData:adata ...

NSdata 与 NSString,Byte数组,UIImage 的相互转换

1. NSData 与 NSString NSData-> NSString NSString *aString = [[NSString alloc] initWithData:adata enc...

NSdata 与 NSString,Byte数组,UIImage 的相互转换

NSdata 与 NSString,Byte数组,UIImage 的相互转换1. NSData 与 NSString NSData-> NSString NSString *aString = [[N...

NSdata 与 NSString,Byte数组,UIImage 的相互转换

NSdata 与 NSString,Byte数组,UIImage 的相互转换
  • phyky
  • phyky
  • 2017年11月16日 19:20
  • 55
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Object-C 中各数据类型转换 NSData转NSString,Byte,UIImage
举报原因:
原因补充:

(最多只允许输入30个字)