The iOS program needs to encode the URL UTF8 when it accesses the HTTP resource, and I have always liked to encode it using NSString's Stringbyaddingpercentescapesusingencoding method. Today, when using analyze to analyze the project, it is suggested that the following method may present a memory leak:
NSString *enstring = (NSString *) cfurlcreatestringbyaddingpercentescapes (Kcfallocatordefault, (CFSTRINGREF) StringUrl, NULL, NULL, KCFSTRINGENCODINGUTF8);
Note that this method is also a coding scheme, on the query for the difference between the two ways, some harvest. Extra learning Uitabbarcontroller hidden Tabbar and Addchildviewcontroller
Stringbyaddingpercentescapesusingencoding method has a problem: does not go to the url "%&?" Such symbols [This is well understood, because it is not good to distinguish whether these special characters are connection symbols or parameter values]. These characters have a special meaning in the URL syntax, and if you have these characters in the URL's parameters, you need to convert to "%+ascii" form. If these characters are present in the arguments, and we use the Stringbyaddingpercentescapesusingencoding method, the server uses the non-escaped & as a delimiter in the argument, resulting in parsing errors. Because I have almost no%& in the parameters of the project, so I always use no problem. But we still have to use the formal way.
In general, they are used:
<textarea class="crayon-plain print-no" style="line-height: 15px !important; overflow: hidden; font-size: 12px !important; z-index: 0; opacity: 0; -moz-tab-size: 4; -o-tab-size: 4; -webkit-tab-size: 4; tab-size: 4;" wrap="soft" readonly="" data-settings="dblclick">cfstringref cfurlcreatestringbyaddingpercentescapes (cfallocatorref allocator, Cfstringref originalString,/* Type of code to be transcoded */Cfstringref characterstoleaveunescaped,/* Indicates the character not escaped */Cfstringref legalurlcharacterstobeescaped,/* indicates the character to be escaped */cfstringencoding encoding); /* Encoding type *</textarea> /
123456 |
CfstringrefCfurlcreatestringbyaddingpercentescapes( CfallocatorrefAllocator, Cfstringreforiginalstring,/ * Type of code to be transcoded * / Cfstringrefcharacterstoleaveunescaped,/ * Indicates non-escaped characters * / Cfstringreflegalurlcharacterstobeescaped,/ * Indicates that the escaped character is determined * / cfstringencodingencoding);/ * Encoding type * / |
The scheme is to encode the parameter values individually (if the entire URL is encoded, the URL delimiter & and so on), and finally stitching into a complete string.
The sample demo is as follows:
<textarea class="crayon-plain print-no" style="line-height: 15px !important; overflow: hidden; font-size: 12px !important; z-index: 0; opacity: 0; -moz-tab-size: 4; -o-tab-size: 4; -webkit-tab-size: 4; tab-size: 4;" wrap="soft" readonly="" data-settings="dblclick">Cfstringref escaped = Cfurlcreatestringbyaddingpercentescapes (null, (CFSTRINGREF) Self, NULL, (CFSTRINGREF) @ "!*" ( );: @&=+$,/?%#[] ", kCFStringEncodingUTF8); NSString *out = [NSString stringwithstring: (NSString *) escaped]; Cfrelease (escaped);//Remember to release</textarea>
123 |
Cfstringrefescaped =Cfurlcreatestringbyaddingpercentescapes(NULL,(Cfstringref) Self,NULL,(Cfstringref)@"!*" ();: @&=+$,/?%#[] ",kCFStringEncodingUTF8); NSString * out= [NSStringstringwithstring:(NSString *)escaped]; Cfrelease(escaped);//Remember to release |
Another small point of knowledge:
The following code is often used to make a picture cut before
<textarea class="crayon-plain print-no" style="line-height: 15px !important; overflow: hidden; font-size: 12px !important; z-index: 0; opacity: 0; -moz-tab-size: 4; -o-tab-size: 4; -webkit-tab-size: 4; tab-size: 4;" wrap="soft" readonly="" data-settings="dblclick">cgimageref newimageref = Cgimagecreatewithimageinrect (sourceimageref, rect); UIImage *newimage = [UIImage imagewithcgimage:newimageref]; </textarea>
12 |
CgimagerefNewimageref =Cgimagecreatewithimageinrect(Sourceimageref,rect); UIImage *NewImage= [UIImageImagewithcgimage:Newimageref]; |
In fact, this code will cause a memory leak, the correct way is to also release Newimageref, as follows:
<textarea class="crayon-plain print-no" style="line-height: 15px !important; overflow: hidden; font-size: 12px !important; z-index: 0; opacity: 0; -moz-tab-size: 4; -o-tab-size: 4; -webkit-tab-size: 4; tab-size: 4;" wrap="soft" readonly="" data-settings="dblclick">cgimageref newimageref = Cgimagecreatewithimageinrect (sourceimageref, rect); UIImage *newimage = [UIImage imagewithcgimage:newimageref]; Cgimagerelease (NEWIMAGEREF); </textarea>
123 |
CgimagerefNewimageref =Cgimagecreatewithimageinrect(Sourceimageref,rect); UIImage *NewImage= [UIImageImagewithcgimage:Newimageref]; Cgimagerelease(Newimageref); |
iOS uses Cfurlcreatestringbyaddingpercentescapes for URL encoding