Link: http://qzc770707.blog.163.com/blog/static/34082753201022442356502/
String storage and reading of uiimage
//
// Nsdataadditions. h
// Iostestpro
//
// Created by infohold infohold_mac3 on 13-2-22.
// Copy right (c) 2013YearInfohold infohold_mac3. All rights reserved.
//
# Import<Foundation/Foundation. h>
@ InterfaceNsdata (mbbase64)
// Get nsdata from base64 string
+ (ID) Datawithbase64encodedstring :(Nsstring*) String;// Padding '= 'characters are optional. whitespace is ignored.
// Turn nsdata into base64 string
-(Nsstring*) Base64encoding;
@ End
// Implementation file
//
// Nsdataadditions. m
// Iostestpro
//
// Created by infohold infohold_mac3 on 13-2-22.
// Copy right (c) 2013YearInfohold infohold_mac3. All rights reserved.
//
# Import"Nsdataadditions. H"
StaticConstCharEncodingtable [] ="Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 + /";
// Encodingtable. Length = 64 = 26 + 26 + 10 + 2
@ ImplementationNsdata (mbbase64)
+ (Nsdata*) Datawithbase64encodedstring :(Nsstring*) String
{
// Create a memory buffer containing base64 encoded string data
If(String =Nil)
[NsexceptionRaise:NsinvalidargumentexceptionFormat:Nil];
If([StringLength] =0)
Return[Nsdata Data];
Static Char* Decodingtable =Null;
If(Decodingtable =Null)
{
Decodingtable =Malloc(256);
If(Decodingtable =Null)
Return Nil;
Memset(Decodingtable,Char_max,256);
NsuintegerI;
For(I =0; I <64; I ++)
Decodingtable [(Short)Encodingtable[I] = I;
}
ConstChar* Characters = [StringCstringusingencoding:Nsasciistringencoding];
If(Characters =Null)// Not an ASCII string!
Return Nil;
Char* Bytes =Malloc([StringLength] +3)/4)*3);
If(Bytes =Null)
Return Nil;
NsuintegerLength =0;
NsuintegerI =0;
While(Yes)
{
CharBuffer [4];
ShortBufferlength;
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[NsdataDatawithbytesnocopy: BytesLength: Length];
}
-(Nsstring*) Base64encoding;
{
If([Self Length] =0)
Return @"";
Char* Characters =Malloc((([Self Length] +2)/3)*4);
If(Characters =Null)
Return Nil;
NsuintegerLength =0;
NsuintegerI =0;
While(I <[Self Length])
{
CharBuffer [3] = {0,0,0};
ShortBufferlength =0;
While(Bufferlength <3& I <[Self Length])
Buffer [bufferlength ++] = ((Char*)[Self Bytes]) [I ++];
// Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.
Characters [length ++] =Encodingtable[(Buffer [0] &0xfc)>2];
Characters [length ++] =Encodingtable[(Buffer [0] &0x03) <4) | (Buffer [1] &0xf0)>4)];
If(Bufferlength>1)
Characters [length ++] =Encodingtable[(Buffer [1] &0x0f) <2) | (Buffer [2] &0xc0)>6)];
ElseCharacters [length ++] ='=';
If(Bufferlength>2)
Characters [length ++] =Encodingtable[Buffer [2] &0x3f];
ElseCharacters [length ++] ='=';
}
Return[[[NsstringAlloc]Initwithbytesnocopy: CharactersLength: LengthEncoding:Nsasciistringencodingfreewhendone:Yes]Autorelease];
}
@ End
TestCode:
-(Void) Viewdidload
{
[SuperViewdidload];
Uiimage* M_image = [Uiimage Imagenamed:@ "Pic_1.jpg"];
Nsdata* M_imagedata =Uiimagejpegrepresentation(M_image,1.0f);
Nsstring* M_base64str = [m_imagedataBase64encoding];
Nsdata* M_imagedatafromstring = [Nsdata Datawithbase64encodedstring: M_base64str];
Uiimage* M_imagen = [Uiimage Imagewithdata: M_imagedatafromstring];
Uiimageview* M_imageview = [[Uiimageview Alloc]Initwithimage: M_imagen];
M_imageview.Frame=Cgrectmake(0,0,320,460);
[Self.View Addsubview: M_imageview];
[M_imageviewRelease];
}
The end!
2013-02-22