简体   繁体   English

在iPhone中使用base64解码时,数据为零

[英]data is nil when using base64 decoding in iPhone

Im parsing(JSON parsing) an image and tried to display in an image view. 我解析(JSON解析)一个图像,并试图在图像视图中显示。

I used the base64 decode method for decoding. 我使用base64解码方法进行解码。

I used the below code for this: 我使用下面的代码:

NSString *base64String = responseObj.content;

   NSData* data = [Base64 decode:base64String];

    image.frame =  CGRectMake(0, 0, 100, 100);
    image.backgroundColor = [UIColor blueColor];

    image.image = [UIImage imageWithData:data];

The Base64 class is Base64类是

.h
#import <Foundation/Foundation.h>


@interface Base64 : NSObject {

}

+ (void) initialize;

+ (NSString*) encode:(const uint8_t*) input length:(NSInteger) length;

+ (NSString*) encode:(NSData*) rawBytes;

+ (NSData*) decode:(const char*) string length:(NSInteger) inputLength;

+ (NSData*) decode:(NSString*) string;

@end
.m
static char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static char decodingTable[128];

+ (void) initialize {
    if (self == [Base64 class]) {
        memset(decodingTable, 0, ArrayLength(decodingTable));
        for (NSInteger i = 0; i < ArrayLength(encodingTable); i++) {
            decodingTable[encodingTable[i]] = i;
        }
    }
}


+ (NSString*) encode:(const uint8_t*) input length:(NSInteger) length {
    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;

    for (NSInteger i = 0; i < length; i += 3) {
        NSInteger value = 0;
        for (NSInteger j = i; j < (i + 3); j++) {
            value <<= 8;

            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }

        NSInteger index = (i / 3) * 4;
        output[index + 0] =                    encodingTable[(value >> 18) & 0x3F];
        output[index + 1] =                    encodingTable[(value >> 12) & 0x3F];
        output[index + 2] = (i + 1) < length ? encodingTable[(value >> 6)  & 0x3F] : '=';
        output[index + 3] = (i + 2) < length ? encodingTable[(value >> 0)  & 0x3F] : '=';
    }

    return [[[NSString alloc] initWithData:data
                                  encoding:NSASCIIStringEncoding] autorelease];
}


+ (NSString*) encode:(NSData*) rawBytes {
    return [self encode:(const uint8_t*) rawBytes.bytes length:rawBytes.length];
}


+ (NSData*) decode:(const char*) string length:(NSInteger) inputLength {
    if ((string == NULL) || (inputLength % 4 != 0)) {
        return nil;
    }

    while (inputLength > 0 && string[inputLength - 1] == '=') {
        inputLength--;
    }

    NSInteger outputLength = inputLength * 3 / 4;
    NSMutableData* data = [NSMutableData dataWithLength:outputLength];
    uint8_t* output = data.mutableBytes;

    NSInteger inputPoint = 0;
    NSInteger outputPoint = 0;
    while (inputPoint < inputLength) {
        char i0 = string[inputPoint++];
        char i1 = string[inputPoint++];
        char i2 = inputPoint < inputLength ? string[inputPoint++] : 'A'; /* 'A' will decode to \0 */
        char i3 = inputPoint < inputLength ? string[inputPoint++] : 'A';

        output[outputPoint++] = (decodingTable[i0] << 2) | (decodingTable[i1] >> 4);
        if (outputPoint < outputLength) {
            output[outputPoint++] = ((decodingTable[i1] & 0xf) << 4) | (decodingTable[i2] >> 2);
        }
        if (outputPoint < outputLength) {
            output[outputPoint++] = ((decodingTable[i2] & 0x3) << 6) | decodingTable[i3];
        }
    }

    return data;
}


+ (NSData*) decode:(NSString*) string {
    return [self decode:[string cStringUsingEncoding:NSASCIIStringEncoding] length:string.length];
}

@end

When i tried to see the contents of NSString it is showing the contents correctly. 当我试图看到NSString的内容时,它正确显示内容。 But when i tried to see the contents of data it is showing 但是当我试图看到它显示的数据内容时

(NSData *) $2 = 0x00000000 . (NSData *)$ 2 = 0x00000000。

Can anyone please tell me where I'm going wrong and why the data is nil. 任何人都可以告诉我我哪里出错了,为什么数据为零。

i had a similar problem with some googled copy&paste base64 encoding method, so i would recommend to use QSUtilities : 我有一些谷歌拷贝和粘贴base64编码方法的类似问题,所以我建议使用QSUtilities

This library provides general purposes libraries (string cleanup, net access, etc.) within Objective-C classes. 该库提供Objective-C类中的通用库(字符串清理,网络访问等)。

Take a look at the QSString Class 看看QSSt​​ring类

Try this: 尝试这个:

 NSString *base64String = responseObj.content;// U have base64 String

 [Base64 initialize]; //U forgot this

 NSData* data = [Base64 decode:base64String];
 image.frame =  CGRectMake(0, 0, 100, 100);
 image.backgroundColor = [UIColor blueColor];
 image.image = [UIImage imageWithData:data];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM