encode ios
2016-12-26 13:50:00 weixin_34294649 阅读数 12

相信大家总会碰到需要encode的时候,接着百度啊,然后发现stringByAddingPercentEscapesUsingEncoding这个方法,但是这个方法有时候无法对+等符号进行encode。那这时候咋办呢?


3259244-2733474e6eb450c0.png


3259244-c271d6afcb38d26c.png


//encode

CF_EXPORT

CFStringRef CFURLCreateStringByAddingPercentEscapes(CFAllocatorRef allocator, CFStringRef originalString, CFStringRef charactersToLeaveUnescaped, CFStringRef legalURLCharactersToBeEscaped, CFStringEncoding encoding);


NSString *encodedValue = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(nil,(CFStringRef)value, nil,(CFStringRef)@"!*'();:@&=+$,/ %#[]", kCFStringEncodingUTF8));

2018-09-29 13:09:59 weixin_33958366 阅读数 27

我从简书搬家啦~~

本篇文章源地址:https://www.jianshu.com/p/397acacbd90d

相信大家总会碰到需要encode的时候,接着百度啊,然后发现stringByAddingPercentEscapesUsingEncoding这个方法,但是这个方法有时候无法对+等符号进行encode。那这时候咋办呢?






//encode

CF_EXPORT

CFStringRef CFURLCreateStringByAddingPercentEscapes(CFAllocatorRef allocator, CFStringRef originalString, CFStringRef charactersToLeaveUnescaped, CFStringRef legalURLCharactersToBeEscaped, CFStringEncoding encoding);


NSString *encodedValue = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(nil,(CFStringRef)value, nil,(CFStringRef)@"!*'();:@&=+$,/ %#[]", kCFStringEncodingUTF8));


2017-09-01 14:46:18 u011270282 阅读数 522

使用libfdk-aac编码。

#import "FDKAACEncoder.h"
#include "aacenc_lib.h"

#define PROFILE_AAC_LC 2
#define PROFILE_AAC_HE 5
#define PROFILE_AAC_HE_v2 29
#define PROFILE_AAC_LD 23
#define PROFILE_AAC_ELD 39


@interface FDKAACEncoder(){
    HANDLE_AACENCODER _encoder;
}

@end


@implementation FDKAACEncoder

- (int)initEncoder:(int)bitrate samplerate:(int)samplerate channel:(int)channel{
    AACENC_ERROR rt = aacEncOpen(&_encoder, 0, 0);
    if (rt != AACENC_OK) {
        NSLog(@"aac enc open error %zd",rt);
        return -1;
    }
    aacEncoder_SetParam(_encoder, AACENC_AOT, PROFILE_AAC_LC);
    aacEncoder_SetParam(_encoder, AACENC_BITRATE, bitrate);
    aacEncoder_SetParam(_encoder, AACENC_SAMPLERATE, samplerate);
    aacEncoder_SetParam(_encoder, AACENC_CHANNELMODE, MODE_2);

    return 0;
}

- (int)encodeToAAC:(char*)pcm len:(int)len{
    
    AACENC_BufDesc in = {0};
    {
        INT bid = IN_AUDIO_DATA;
        INT elSize = 2;
        void *buf[1] = {pcm};
        in.bufs = buf;
        in.numBufs = 1;
        in.bufferIdentifiers = &bid;
        in.bufElSizes = &elSize;
    }
    
    AACENC_BufDesc out = {0};
    {
        INT size = 1024;
        INT bid = OUT_BITSTREAM_DATA;
        void *buf[1] = {malloc(1024)};
        INT elSize = 1;
        
        out.bufs = buf;
        out.bufSizes = &size;
        out.numBufs = 1;
        out.bufferIdentifiers = &bid;
        out.bufElSizes = &elSize;
    }
    
    AACENC_InArgs inArgs = {0};
    inArgs.numInSamples = (INT)len/2;
    
    AACENC_OutArgs outArgs = {0};
    
    AACENC_ERROR rt = aacEncEncode(_encoder, &in, &out, &inArgs, &outArgs);
    if (rt != AACENC_OK) {
        NSLog(@"aac enc encode error %zd",rt);
    }else{
        NSLog(@"aac enc encode size %zd %zd",outArgs.numOutBytes, out.bufSizes[0]);
    }

    if (_delegate && [_delegate respondsToSelector:@selector(onAudioEncBuf:len:)]) {
        [_delegate onAudioEncBuf:out.bufs[0] len:outArgs.numOutBytes];
    }
    
    if (out.bufs[0]) {
        free(out.bufs[0]);
    }
    
    return 0;
}

- (void)free{
    
    if (_encoder) {
        aacEncClose(&_encoder);
        _encoder = NULL;
    }
}

@end


2013-07-27 19:17:46 snsogblgbl 阅读数 818

用ASIFormDataRequest请求URl地址,当使用GET传参,参数是普通中文可以用  NSString 的 stringByAddingPercentEscapesUsingEncoding 可以对 url 参数进行编码。但是如果参数中包含特殊符号如 " & ? ! "等等,就不能用这种方法。

包含了特殊符号的可以使用ASIFormDataRequest 的 encodeURL 方法 如下使用;

ASIFormDataRequest* request = [[ASIFormDataRequest alloc] initWithURL:nil];
place_name = [request encodeURL:place_name];


encodeURl 方法如下:

- (NSString*)encodeURL:(NSString *)string
{
	NSString *newString = [NSMakeCollectable(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding([self stringEncoding]))) autorelease];
	if (newString) {
		return newString;
	}
	return @"";
}




2013-11-27 15:03:45 qingjoin1 阅读数 812
//ios Url Encode 
//有时候在请求的参数里里特殊符号比如“+”等。而如果没有encode的话那么传过去的还是”  ”,面实际上是%2B。
-(NSString*)UrlValueEncode:(NSString*)str
{
        NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                               (CFStringRef)str,
                                                                               NULL,
                                                                                  CFSTR("!*'();:@&=+$,/?%#[]"),
                                                                               kCFStringEncodingUTF8);
       [result autorelease];
       return result;
}



//- (NSString*)URLDecodedString:(NSString*)str
//{
////    NSString *result = [[self stringByReplacingOccurrencesOfString:@"%0D%0A" withString:@""] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    NSString *result = [[str stringByReplacingOccurrencesOfString:@"%0D%0A" withString:@""] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    return [result stringByReplacingOccurrencesOfString:@"+" withString:@" "];
//}

iOS URL encode

阅读数 456

ios Url Encode

阅读数 12

没有更多推荐了,返回首页