IOS uses CFURLCreateStringByAddingPercentEscapes for URL Encoding

Source: Internet
Author: User

IOS uses CFURLCreateStringByAddingPercentEscapes for URL Encoding
Zookeeper

When the iOS program accesses HTTP resources, the URL must be UTF-8 encoded. I have always liked to use the stringByAddingPercentEscapesUsingEncoding method of NSString for encoding. When you use Analyze to Analyze the project today, the following method may cause memory leakage:
NSString * enString = (NSString *) CFURLCreateStringByAddingPercentEscapes (kCFAllocatorDefault, (CFStringRef) stringURL, NULL, NULL, kCFStringEncodingUTF8 );
Note that this method is also an encoding scheme, and the difference between the two methods is queried, some gains. Learn more about hiding tabBar and addChildViewController in UITabBarController.
The stringByAddingPercentEscapesUsingEncoding method has the following problem: "% &?" in the URL will not be transferred &?" And other symbols [This is easy to understand, because it is difficult to distinguish whether these special characters are connected symbols or parameter values]. These characters have special meanings in the URL syntax. If these characters are included in the URL parameters, they must be converted to the "% + ASCII" format. If these characters exist in the parameter and the stringByAddingPercentEscapesUsingEncoding method is used, the server will use the unescaped & In the parameter as the separator, resulting in an analysis error. Because there are almost no % & and other symbols in the parameters in my project, it is okay to use them all the time. But we still need to use the formal method.
Generally, it is used:

CFStringRef escape (CFAllocatorRef allocator, CFStringRef originalString,/* type of transcoding */CFStringRef charactersToLeaveUnescaped,/* indicates the character not escaped */CFStringRef escape, /* indicates the Escape Character */CFStringEncoding encoding);/* encoding type */
123456 Encode (CFAllocatorRef allocator, CFStringReforiginalString,/* type of transcoding */CFStringRef charactersToLeaveUnescaped,/* character indicating not to be escaped */escape, /* indicates the Escape Character */CFStringEncoding encoding);/* encoding type */

The solution is to encode the parameter values separately (URL delimiters and so on will also be encoded if the whole URL is encoded), and finally splice them into a complete string.
The Demo is as follows:

CFStringRef escaped = CFURLCreateStringByAddingPercentEscapes (NULL, (CFStringRef) self, NULL, (CFStringRef )@"! * '();: @ & = + $ ,/? % # [] ", KCFStringEncodingUTF8); NSString * out = [NSString stringWithString :( NSString *) escaped]; CFRelease (escaped); // remember to release
123 CFStringRefescaped = CFURLCreateStringByAddingPercentEscapes (NULL, (CFStringRef) self, NULL, (CFStringRef )@"! * '();: @ & = + $ ,/? % # [] ", KCFStringEncodingUTF8); NSString * out = [NSStringstringWithString :( NSString *) escaped]; CFRelease (escaped); // remember to release

Another small knowledge point:
The following code is often used for image cutting.

CGImageRef newImageRef = CGImageCreateWithImageInRect (sourceImageRef, rect); UIImage * newImage = [UIImage imageWithCGImage: newImageRef];
12 CGImageRefnewImageRef = CGImageCreateWithImageInRect (sourceImageRef, rect); UIImage * newImage = [uiimagewithcgimage: newImageRef];

In fact, this Code may cause memory leakage. The correct method is to release newImageRef, as shown below:

CGImageRef newImageRef = CGImageCreateWithImageInRect (sourceImageRef, rect); UIImage * newImage = [UIImage imageWithCGImage: newImageRef]; cgimagerelef (newImageRef );
123 CGImageRefnewImageRef = CGImageCreateWithImageInRect (sourceImageRef, rect); UIImage * newImage = [UIImageimageWithCGImage: newImageRef]; cgimagerelef (newImageRef );

Related Article

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.