iOS UIImage 和 NSString的相互转化
2014-07-22 10:57
204 查看
首先需要定义一个由NSString类转化为NSData类的方法
+ (NSData *)dataWithBase64EncodedString:(NSString *)string
{
static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
if (string == nil)
[NSException raise:NSInvalidArgumentException format: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;
}
const char *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];
}
bytes = realloc(bytes, length);
return [NSData dataWithBytesNoCopy:bytes length:length];
}
相互转化:
UIImage* pic;
NSData* pictureData = UIImagePNGRepresentation(pic);
NSString* pictureDataString = [pictureData base64Encoding];
UIImage* image = [UIImage imageWithData:[PlantListViewController dataWithBase64EncodedString:pictureDataString]];
+ (NSData *)dataWithBase64EncodedString:(NSString *)string
{
static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
if (string == nil)
[NSException raise:NSInvalidArgumentException format: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;
}
const char *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];
}
bytes = realloc(bytes, length);
return [NSData dataWithBytesNoCopy:bytes length:length];
}
相互转化:
UIImage* pic;
NSData* pictureData = UIImagePNGRepresentation(pic);
NSString* pictureDataString = [pictureData base64Encoding];
UIImage* image = [UIImage imageWithData:[PlantListViewController dataWithBase64EncodedString:pictureDataString]];
相关文章推荐
- 字符串编码转换(GBK) NSString, char, NSData相互转化
- NSString NSNumber 相互转化
- 字符串编码转换(GBK) NSString, char, NSData相互转化 从 GBK 转到&nb
- NSArray与NSString、NSData,NSDictionary与NSString、NSData 相互转化
- 字符串编码转换(GBK) NSString, char, NSData相互转化 从 GBK 转到 UTF-8
- NSString和NSDate的相互转化
- NSString和NSNumber的相互转化
- iphone之NSString, char, NSData相互转化
- UTF-8 和 GBK 的 NSString 相互转化的方法
- UTF-8 和 GBK 的 NSString 相互转化的方法
- NSArray与NSString、NSData,NSDictionary与NSString、NSData 相互转化
- NSString和NSDate的相互转化
- UTF-8 和 GBK 的 NSString 相互转化的方法
- UTF-8 和 GBK 的 NSString 相互转化的方法
- ios开发中object-c中UTF-8 和 GBK 的 NSString 相互转化的方法
- NSString, char, NSData,UIImage相互转化
- iphone之NSString, char, NSData相互转化
- NDData* 和 NSString* 以及NSSting* 和 char* 相互转化
- iphone之NSString, char, NSData相互转化
- NSDate与 NSString 、long long类型的相互转化