Convert nsdata to nsstring, byte, and uiimage in object-C

Source: Internet
Author: User

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: 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
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

 1 -(Nsstring *) filepathwithname :( nsstring * ) Filename  2   {  3 Nsarray * paths =Nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );  4 Nsstring * documentsdirectory = [paths objectatindex: 0  ];  5           Return  [Documentsdirectory stringbyappendingpathcomponent: Filename];  6   }  7   8 -(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary * ) Launchoptions { 9       //  Audio File Path  10 Nsstring * mp3path1 = [[nsbundle mainbundle] pathforresource: @"  1  " Oftype: @"  MP3  "  ];  11 Nsstring * mp3path2 = [[nsbundle mainbundle] pathforresource: @"  2  " Oftype: @"  MP3  "  ];  12           //  Audio Data  13 Nsdata * sound1data = [[Nsdata alloc] initwithcontentsoffile: mp3path1];  14 Nsdata * sound2data = [[Nsdata alloc] initwithcontentsoffile: mp3path2];  15           //  Merge audio 16 Nsmutabledata * Sounds = [Nsmutabledata alloc];  17   [Sounds appenddata: sound1data];  18   [Sounds appenddata: sound2data];  19           //  Save audio  20   21 Nslog ( @"  Data Length: % d  "  , [Sounds length]); 22   23 [Sounds writetofile: [self filepathwithname: @"  Tmp133  "  ] Atomically: Yes];  24           25   [Window makekeyandvisible];  26       27       Return  Yes;  28 }

6. nsstring merge

1 Nsstring * String ; //  Result string  2 Nsstring * string1, string2; //  The existing string. You need to connect string1 and string2.  3   //  Method 1.  4   String = [Nsstring initwithformat: @"  % @, % @  " , String1, string2];  5   //  Method 2.  6   String = [String1 stringbyappendingstring: string2];  7   //  Method 3.  8   String = [ String Stringbyappendingformat: @"  % @, % @ " , String1, string2];

 

 


 

 

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.