0

When I convert my NSMutableArray Into NSdata,I get NSMutableArray data in bytes. Now I want To convert it into UIImage, because I want to send My Array data by Email, but I get null in UIImage.

Here is my code.

 NSData *data = [NSKeyedArchiver archivedDataWithRootObject:viewArray]; UIImage *image = [UIImage imageWithData:data]; [controller addAttachmentData:image mimeType:@"image/png" fileName:@"labelData"]; 

1 Answer 1

1

To convert an image to string you need a method to convert NSData to a base64Encoded string and back (lots of examples http://cocoadev.com/wiki/BaseSixtyFour). The easiest ones to use are categories on NSData so you can do something like this:

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','+','/' }; 

Try this way may help you out.

Sign up to request clarification or add additional context in comments.

5 Comments

I hope now will surely work, use the edited code. Let me know if any issue occurs
I hope this will work now, as I have edited it not getting any error now. Actually I have replicate same scenario for this everytime in the demo app and thats why sometimes error occurs.
Thanx dear.i apply your are editing code.i got no error.but there is some worning ..Note List[459:207]+[NSData dataFromBase64EncodedString:]: unrecognized selector sent to class 0x26aea74*** Terminating app due to uncaught exception 'NSInvalidArgumentException',reason: '+[NSData dataFromBase64EncodedString:]: unrecognized selector sent to class 0x26aea74'
Dear if you free ..just visit my profile.i post this question with detail ,i think if you check this ,you will suggest better solution for me..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.