爲了將圖像轉換爲字符串,你需要一種方法來的NSData轉換爲字符串base64Encoded和背面(很多例子http://cocoadev.com/wiki/BaseSixtyFour)。最簡單的使用NSData的類別,所以你可以這樣做:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:viewArray];
NSString* pictureDataString = [self base64EncodingWithLineLength:data];
UIImage* image = [UIImage imageWithData:[NSData
dataFromBase64EncodedString: pictureDataString]];
[controller addAttachmentData:image mimeType:@"image/png"
fileName:@"labelData"];
- (NSString *) base64EncodingWithLineLength:(NSData*) data {
const unsigned char* bytesArr=[data bytes];
unsigned int lineLength=0;
NSMutableString *result = [NSMutableString stringWithCapacity:[data length]];
unsigned long ixtext = 0;
unsigned long lentext = [data length];
long ctremaining = 0;
unsigned char inbuf[3], outbuf[4];
unsigned short i = 0;
unsigned short charsonline = 0, ctcopy = 0;
unsigned long ix = 0;
while(YES) {
ctremaining = lentext - ixtext;
if(ctremaining <= 0) break;
for(i = 0; i < 3; i++) {
ix = ixtext + i;
if(ix < lentext) inbuf[i] = bytesArr[ix];
else inbuf [i] = 0;
}
outbuf [0] = (inbuf [0] & 0xFC) >> 2;
outbuf [1] = ((inbuf [0] & 0x03) << 4) | ((inbuf [1] & 0xF0) >> 4);
outbuf [2] = ((inbuf [1] & 0x0F) << 2) | ((inbuf [2] & 0xC0) >> 6);
outbuf [3] = inbuf [2] & 0x3F;
ctcopy = 4;
switch(ctremaining) {
case 1:
ctcopy = 2;
break;
case 2:
ctcopy = 3;
break;
}
for(i = 0; i < ctcopy; i++)
[result appendFormat:@"%c", encodingTable[outbuf[i]]];
for(i = ctcopy; i < 4; i++)
[result appendString:@"="];
ixtext += 3;
charsonline += 4;
if(lineLength > 0) {
if(charsonline >= lineLength) {
charsonline = 0;
[result appendString:@"\n"];
}
}
}
return [NSString stringWithString:result];
}
static char encodingTable[64] = {
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/' };
試試這種方式可能會幫助你。
添加該方法。請在編輯的代碼中找到它。 – Kuldeep
我希望現在肯定會工作,使用編輯後的代碼。如果發生任何問題,請告知我 – Kuldeep
我希望現在能夠正常工作,因爲我已經編輯了它,現在沒有發生任何錯誤。實際上,我每次在演示應用程序中都會複製相同的場景,這就是爲什麼有時會出現錯誤。 – Kuldeep