在Objective C中, 使用如下的方法可以在二进制数据(NSDate类型)和based64 encoded数据(保存在NSString中)相互转换.
// From binary data to base64 encoded string NSString string; if ([data respondsToSelector:@selector(base64EncodedStringWithOptions:)]) { string = [data base64EncodedStringWithOptions:kNilOptions]; // iOS 7+ } else { string = [data base64Encoding]; // pre iOS7 } // From base64 encoded string to binary data NSData *data; if ([NSData instancesRespondToSelector:@selector(initWithBase64EncodedString:options:)]) { data = [[NSData alloc] initWithBase64EncodedString:string options:kNilOptions]; // iOS 7+ } else { data = [[NSData alloc] initWithBase64Encoding:string]; // pre iOS7 }
No comments:
Post a Comment