1. nsdata and nsstring
Nsdata --> nsstring
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: nsutf8stringencoding];
Byte * testbyte = (byte *) [testdata bytes];
Byte --> nsdata
Byte byte [] = {, 21 };
Nsdata * aData = [[nsdata alloc] initwithbytes: Byte Length: 24];
3. nsdata and uiimage
Nsdata --> uiimage
Uiimage * aimage = [uiimage imagewithdata: imagedata];
// Example: extract 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
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 = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes); nsstring * documentsdirectory = [paths objectatindex: 0]; return [documentsdirectory stringbyappendingpathcomponent: Filename];}-(bool) Application :( uiapplication *) Application didfinishlaunchingwithoptions :( 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] Audio: 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: @ "tmp1_"] atomically: Yes]; [Window makekeyandvisible]; return yes ;}
6 nsstring merge
Nsstring * string; // result string nsstring * string1, string2; // an existing string. You must connect string1 and string2. // method 1. string = [nsstring initwithformat: @ "% @, % @", string1, string2]; // method 2. string = [string1 stringbyappendingstring: string2]; // method 3. string = [String stringbyappendingformat: @ "% @, % @", string1, string2];