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 ); |