iOS数据类型转换 NSData转NSString,Byte,UIImage

iOS数据类型转换 NSData转NSString,Byte,UIImage,第1张

概述iOS数据类型转换 NSData转NSString,Byte,UIImage

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

1,NSData 与 Nsstring  NSData --> Nsstring  Nsstring *aString = [[Nsstring alloc] initWithData:adata enCoding:NSUTF8StringEnCoding];  Nsstring --> NSData  Nsstring *aString = @"1234";  NSData *aData = [aString dataUsingEnCoding: NSUTF8StringEnCoding];   iOS Nsstring 和NSData 转换   Nsstring 转换成NSData 对象    NSData* xmlData = [@"testdata" dataUsingEnCoding:NSUTF8StringEnCoding];    NSData 转换成Nsstring对象    NSData * data;    Nsstring *result = [[Nsstring alloc] initWithData:data  enCoding: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; }

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的iOS数据类型转换 NSData转NSString,Byte,UIImage全部内容,希望文章能够帮你解决iOS数据类型转换 NSData转NSString,Byte,UIImage所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1105822.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存