iphone - UIImage to base64 not working -
this question has answer here:
- convert between uiimage , base64 string 13 answers
i'm trying convert uiimage base64 string , upload it.
this code:
nsdata *imagedata = uiimagejpegrepresentation(self.image, 0.2); imagedatabase64 = [nsstring stringwithformat:@"data:image/jpeg;base64,%@", [imagedata base64encodedstring]]; nsurl *url = [nsurl urlwithstring:imagedatabase64]; nsdata *imgdata = [nsdata datawithcontentsofurl:url]; uiimage *ret = [uiimage imagewithdata:imgdata]; [imageview setimage:ret]; when set imagedatabase64 nsstring working.
update:
base64encodedstring code:
.h
void *newbase64decode( const char *inputbuffer, size_t length, size_t *outputlength); char *newbase64encode( const void *inputbuffer, size_t length, bool separatelines, size_t *outputlength); @interface nsdata (base64) + (nsdata *)datafrombase64string:(nsstring *)astring; - (nsstring *)base64encodedstring; @end .m
// // mapping 6 bit pattern ascii character. // static unsigned char base64encodelookup[65] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"; // // definition "masked-out" areas of base64decodelookup mapping // #define xx 65 // // mapping ascii character 6 bit pattern. // static unsigned char base64decodelookup[256] = { xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 62, xx, xx, xx, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, xx, xx, xx, xx, xx, xx, xx, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, xx, xx, xx, xx, xx, xx, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, }; // // fundamental sizes of binary , base64 encode/decode units in bytes // #define binary_unit_size 3 #define base64_unit_size 4 // // newbase64decode // // decodes base64 ascii string in inputbuffer newly malloced // output buffer. // // inputbuffer - source ascii string decode // length - length of string or -1 (to specify strlen should used) // outputlength - if not-null, on output contain decoded length // // returns decoded buffer. must free'd caller. length given // outputlength. // void *newbase64decode( const char *inputbuffer, size_t length, size_t *outputlength) { if (length == -1) { length = strlen(inputbuffer); } size_t outputbuffersize = ((length+base64_unit_size-1) / base64_unit_size) * binary_unit_size; unsigned char *outputbuffer = (unsigned char *)malloc(outputbuffersize); size_t = 0; size_t j = 0; while (i < length) { // // accumulate 4 valid characters (ignore else) // unsigned char accumulated[base64_unit_size]; size_t accumulateindex = 0; while (i < length) { unsigned char decode = base64decodelookup[inputbuffer[i++]]; if (decode != xx) { accumulated[accumulateindex] = decode; accumulateindex++; if (accumulateindex == base64_unit_size) { break; } } } // // store 6 bits each of 4 characters 3 bytes // // (uses improved bounds checking suggested alexandre colucci) // if(accumulateindex >= 2) outputbuffer[j] = (accumulated[0] << 2) | (accumulated[1] >> 4); if(accumulateindex >= 3) outputbuffer[j + 1] = (accumulated[1] << 4) | (accumulated[2] >> 2); if(accumulateindex >= 4) outputbuffer[j + 2] = (accumulated[2] << 6) | accumulated[3]; j += accumulateindex - 1; } if (outputlength) { *outputlength = j; } return outputbuffer; } // // newbase64encode // // encodes arbitrary data in inputbuffer base64 newly malloced // output buffer. // // inputbuffer - source data encode // length - length of input in bytes // separatelines - if zero, no cr/lf characters added. otherwise // cr/lf pair added every 64 encoded chars. // outputlength - if not-null, on output contain encoded length // (not including terminating 0 char) // // returns encoded buffer. must free'd caller. length given // outputlength. // char *newbase64encode( const void *buffer, size_t length, bool separatelines, size_t *outputlength) { const unsigned char *inputbuffer = (const unsigned char *)buffer; #define max_num_padding_chars 2 #define output_line_length 64 #define input_line_length ((output_line_length / base64_unit_size) * binary_unit_size) #define cr_lf_size 2 // // byte accurate calculation of final buffer size // size_t outputbuffersize = ((length / binary_unit_size) + ((length % binary_unit_size) ? 1 : 0)) * base64_unit_size; if (separatelines) { outputbuffersize += (outputbuffersize / output_line_length) * cr_lf_size; } // // include space terminating 0 // outputbuffersize += 1; // // allocate output buffer // char *outputbuffer = (char *)malloc(outputbuffersize); if (!outputbuffer) { return null; } size_t = 0; size_t j = 0; const size_t linelength = separatelines ? input_line_length : length; size_t lineend = linelength; while (true) { if (lineend > length) { lineend = length; } (; + binary_unit_size - 1 < lineend; += binary_unit_size) { // // inner loop: turn 48 bytes 64 base64 characters // outputbuffer[j++] = base64encodelookup[(inputbuffer[i] & 0xfc) >> 2]; outputbuffer[j++] = base64encodelookup[((inputbuffer[i] & 0x03) << 4) | ((inputbuffer[i + 1] & 0xf0) >> 4)]; outputbuffer[j++] = base64encodelookup[((inputbuffer[i + 1] & 0x0f) << 2) | ((inputbuffer[i + 2] & 0xc0) >> 6)]; outputbuffer[j++] = base64encodelookup[inputbuffer[i + 2] & 0x3f]; } if (lineend == length) { break; } // // add newline // outputbuffer[j++] = '\r'; outputbuffer[j++] = '\n'; lineend += linelength; } if (i + 1 < length) { // // handle single '=' case // outputbuffer[j++] = base64encodelookup[(inputbuffer[i] & 0xfc) >> 2]; outputbuffer[j++] = base64encodelookup[((inputbuffer[i] & 0x03) << 4) | ((inputbuffer[i + 1] & 0xf0) >> 4)]; outputbuffer[j++] = base64encodelookup[(inputbuffer[i + 1] & 0x0f) << 2]; outputbuffer[j++] = '='; } else if (i < length) { // // handle double '=' case // outputbuffer[j++] = base64encodelookup[(inputbuffer[i] & 0xfc) >> 2]; outputbuffer[j++] = base64encodelookup[(inputbuffer[i] & 0x03) << 4]; outputbuffer[j++] = '='; outputbuffer[j++] = '='; } outputbuffer[j] = 0; // // set output length , return buffer // if (outputlength) { *outputlength = j; } return outputbuffer; } @implementation nsdata (base64) // // datafrombase64string: // // creates nsdata object containing base64 decoded representation of // base64 string 'astring' // // parameters: // astring - base64 string decode // // returns autoreleased nsdata representation of base64 string // + (nsdata *)datafrombase64string:(nsstring *)astring { nsdata *data = [astring datausingencoding:nsasciistringencoding]; size_t outputlength; void *outputbuffer = newbase64decode([data bytes], [data length], &outputlength); nsdata *result = [nsdata datawithbytes:outputbuffer length:outputlength]; free(outputbuffer); return result; } // // base64encodedstring // // creates nsstring object contains base 64 encoding of // receiver's data. lines broken @ 64 characters long. // // returns autoreleased nsstring being base 64 representation of // receiver. // - (nsstring *)base64encodedstring { size_t outputlength; char *outputbuffer = newbase64encode([self bytes], [self length], true, &outputlength); nsstring *result = [[[nsstring alloc] initwithbytes:outputbuffer length:outputlength encoding:nsasciistringencoding] autorelease]; free(outputbuffer); return result; } @end
duplicated question! try link: convert between uiimage , base64 string
in summary need encode nsdata string using base64 category such https://github.com/l4u/nsdata-base64/
the category provides following methods nsdata class can use convert nsdata nsstring (base 64 encoding) , forward.
+ (nsdata *)datafrombase64string:(nsstring *)astring; - (nsstring *)base64encodedstring; try changing line:
imagedatabase64 = [nsstring stringwithformat:@"data:image/jpeg;base64,%@", [imagedata base64encodedstring]] to
nsstring *imagedatabase64encodedstring = [imagedata base64encodedstring]; to decode can use opposite method:
nsdata *theimagedata = [nsdata datafrombase64string:imagedatabase64encodedstring]; uiimage *ret = [uiimage theimagedata];
Comments
Post a Comment