博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

算法--Base64与UIImage的转换

Posted on 2013-02-22 19:15  星尘的天空  阅读(520)  评论(0编辑  收藏  举报

链接地址:http://qzc770707.blog.163.com/blog/static/34082753201022442356502/

 

UIImage 的字符串存储与读取

 

//

//  NSDataAdditions.h

//  IOSTestPro

//

//  Created by infohold infohold_Mac3 on 13-2-22.

//  Copyright (c) 2013 infohold infohold_Mac3. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 @interface NSData (MBBase64)

//Get NSData from Base64 String

+ (id)dataWithBase64EncodedString:(NSString *)string;//Padding '=' characters are optional. Whitespace is ignored.

//Turn NSData into Base64 String

- (NSString *)base64Encoding;

@end

 

 

 

 

 

//实现文件

//

//  NSDataAdditions.m

//  IOSTestPro

//

//  Created by infohold infohold_Mac3 on 13-2-22.

//  Copyright (c) 2013 infohold infohold_Mac3. All rights reserved.

//

 

#import "NSDataAdditions.h"

 

staticconstchar encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

//encodingTable.length = 64 = 26+26+10+2

 

@implementation NSData (MBBase64)

+ (NSData *) dataWithBase64EncodedString: (NSString *)string

{

    // Create a memory buffer containing Base64 encoded string data

    if (string == nil)

        [NSExceptionraise:NSInvalidArgumentExceptionformat:nil];

    if ([string length] == 0)

        return [NSData data];

    

    static char *decodingTable = NULL;

    if (decodingTable == NULL)

    {

        decodingTable = malloc(256);

        if (decodingTable == NULL)

            return nil;

        memset(decodingTable, CHAR_MAX, 256);

        NSUInteger i;

        for (i = 0; i < 64; i++)

            decodingTable[(short)encodingTable[i]] = i;

    }

    

    constchar *characters = [string cStringUsingEncoding:NSASCIIStringEncoding];

    if (characters == NULL)     //  Not an ASCII string!

        return nil;

    char *bytes = malloc((([string length] + 3) / 4) * 3);

    if (bytes == NULL)

        return nil;

    NSUInteger length = 0;

    

    NSUInteger i = 0;

    while (YES)

    {

        char buffer[4];

        short bufferLength;

        for (bufferLength = 0; bufferLength < 4; i++)

        {

            if (characters[i] == '\0')

                break;

            if (isspace(characters[i]) || characters[i] == '=')

                continue;

            buffer[bufferLength] = decodingTable[(short)characters[i]];

            if (buffer[bufferLength++] == CHAR_MAX)      //  Illegal character!

            {

                free(bytes);

                return nil;

            }

        }

        

        if (bufferLength == 0)

            break;

        if (bufferLength == 1)      //  At least two characters are needed to produce one byte!

        {

            free(bytes);

            return nil;

        }

        

        //  Decode the characters in the buffer to bytes.

        bytes[length++] = (buffer[0] << 2) | (buffer[1] >> 4);

        if (bufferLength > 2)

            bytes[length++] = (buffer[1] << 4) | (buffer[2] >> 2);

        if (bufferLength > 3)

            bytes[length++] = (buffer[2] << 6) | buffer[3];

    }

    

    realloc(bytes, length);

    return [NSDatadataWithBytesNoCopy:bytes length:length];

}

 

 

- (NSString *)base64Encoding;

{

    if ([self length] == 0)

        return @"";

    

    char *characters = malloc((([self length] + 2) / 3) * 4);

    if (characters == NULL)

        return nil;

    NSUInteger length = 0;

    

    NSUInteger i = 0;

    while (i < [self length])

    {

        char buffer[3] = {0,0,0};

        short bufferLength = 0;

        while (bufferLength < 3 && i < [self length])

            buffer[bufferLength++] = ((char *)[self bytes])[i++];

        

        //  Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.

        characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];

        characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];

        if (bufferLength > 1)

            characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];

        else characters[length++] = '=';

        if (bufferLength > 2)

            characters[length++] = encodingTable[buffer[2] & 0x3F];

        else characters[length++] = '='

    }

    

    return [[[NSStringalloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncodingfreeWhenDone:YES] autorelease];

}

@end

 

 

测试代码:

- (void)viewDidLoad

{

    [superviewDidLoad];

 

    UIImage *m_image = [UIImage imageNamed:@"pic_1.jpg"];

    NSData *m_imageData = UIImageJPEGRepresentation(m_image, 1.0f);

    NSString *m_Base64Str = [m_imageData base64Encoding];

    

    NSData *m_imageDataFromString = [NSData dataWithBase64EncodedString:m_Base64Str];

    

    UIImage *m_imageN  = [UIImage imageWithData:m_imageDataFromString];

    UIImageView *m_imageView =[[UIImageView alloc] initWithImage:m_imageN];

    m_imageView.frame = CGRectMake(0, 0, 320, 460);

    [self.view addSubview:m_imageView];

    [m_imageView release];

}

 

THE END!

2013-02-22