在服務器中,圖像以二進制格式存儲。我必須使用json在iphone中檢索圖像。我怎樣才能做到這一點?是否有可能使用NSData來做到這一點?如何在服務器中以二進制格式在iPhone中檢索圖像
-1
A
回答
0
您必須使用json解析從服務器獲取二進制值,然後將該字符串轉換爲NSData。
這是用於將base64字符串轉換爲NSData的標準代碼。
//MBBase64.h
@interface NSData (MBBase64)
+ (id)dataWithBase64EncodedString:(NSString *)string; // Padding '=' characters are optional. Whitespace is ignored.
@end
//MBBase64.m
static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/";
@implementation NSData (MBBase64)
+ (id)dataWithBase64EncodedString:(NSString *)string;
{
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];
}
realloc(bytes, length);
return [NSData dataWithBytesNoCopy:bytes length:length];
}
@end
然後加載導致的NSData中的UIImageView
yourimageview.image = [[UIImage alloc] initWithData:resultdata];
1
是的,你需要的二進制數據隱蔽到NSData的是這樣的:
NSData *imgData = [NSData dataWithBase64EncodedString:yourelement];
UIImage *theImg = [UIImage imageWithData:imgData];
您需要MBBase64類,這是可以在這裏:https://github.com/jerrykrinock/CategoriesObjC
相關問題
- 1. 如何在iOS中將圖像轉換爲二進制格式?
- 2. 如何在vb.net中從數據庫檢索二進制圖像並在網格視圖中插入圖像
- 3. 如何在MATLAB中乘以二進制圖像和RGB圖像?
- 4. 如何從Android Web服務中檢索二進制數據?
- 5. 如何在Python中以二進制格式讀取二進制文件?
- 6. 以HTTP格式從HTTP服務器接收二進制數據
- 7. 使用像在sql服務器中查詢二進制列
- 8. 二進制圖像從服務器加載到圖像與JavaScript
- 9. 獲取在datagridview中顯示圖像的二進制格式
- 10. 如何在C++中以二進制格式打包數據
- 11. 我如何可以搜索在二進制格式提供的圖像,如果存在我想在GridView的
- 12. C++如何以二進制格式
- 13. 如何在iphone中以JPG或PNG格式保存圖像?
- 14. 如何檢索服務器中的圖像資源位置
- 15. 如何在cakePHP中將圖像數據從二進制解碼爲png格式。
- 16. 可以在Javascript中將圖像文件轉換爲二進制格式。
- 17. 在SQL服務器中追加圖像數據類型的二進制數據
- 18. 如何在ASP.NET中使用C#從數據庫中檢索二進制映像
- 19. 二進制陣列以圖像在asp.net
- 20. 在SQl服務器中將十進制轉換爲HH.MM格式
- 21. Perl-CGI圖像在瀏覽器中以二進制形式顯示
- 22. 在julia中讀取二進制圖像
- 23. 如何在iphone中搜索圖像
- 24. 如何從二進制文件中檢索整數值在C#
- 25. 如何在Java中使用HttpClient來檢索二進制文件?
- 26. java中的二進制搜索猜謎遊戲服務器端
- 27. 如何在服務器中存儲大量的圖像,以方便在html頁面中檢索?
- 28. 如何從數據庫中檢索圖像(存儲的圖像是二進制形式)
- 29. 快速查詢以檢索圖像加載的二進制圖像?
- 30. Java - 二進制圖像像素索引
API後端哪種語言使用php或其他東西 –
你爲什麼不接受答案?有什麼不對嗎? – 2014-01-13 05:26:05