uiimage - how to convert byte array to image in ios -
today task convert byte array image
first try convert image byte array :-
for converting image byte array first have convert particular image [uiimage] nsdata.then convert nsdata byte array. here give sample code, go through...
//converting uiimage nsdata uiimage *image = [uiimage imagenamed: @"photo-04.jpg"]; nsdata *imagedata = uiimagepngrepresentation(image); //converting nsdata byte array nsuinteger len = [imagedata length]; nslog(@"byte lendata1 %lu",(unsigned long)len); byte *bytedata = (byte*)malloc(len); memcpy(bytedata, [imagedata bytes], len); i try convert byte imageview
const unsigned char *bytes = [imagedata bytes]; nsuinteger length = [imagedata length]; nsmutablearray *bytearray = [nsmutablearray array]; (nsuinteger = 0; < length; i++) { [bytearray addobject:[nsnumber numberwithunsignedchar:bytes[i]]]; } nsdictionary *dictjson = [nsdictionary dictionarywithobjectsandkeys: bytearray, @"photo", nil]; nsdata *jsondata = [nsjsonserialization datawithjsonobject:dictjson options:0 error:null]; nslog(@""); uiimage *image1 = [uiimage imagewithdata:jsondata]; uiimageview *imgview = [[uiimageview alloc] initwithframe:cgrectmake(10, 10, 100, 50)]; imgview.image=image1; i got output convert image byte array want convert byte array image please me in advanced.
first, need convert bytes nsdata
nsdata *imagedata = [nsdata datawithbytes:bytesdata length:length]; then, convert data image.
uiimage *image = [uiimage imagewithdata:imagedata]; and suggest should first searching documentations when problems occur.
here all:
uiimage *image = [uiimage imagenamed:@"rac.png"]; nsdata *imagedata = uiimagepngrepresentation(image); // uiimagejpgrepresentation work nsinteger length = [imagedata length]; byte *bytedata = (byte*)malloc(length); memcpy(bytedata, [imagedata bytes], length); nsdata *newdata = [nsdata datawithbytes:bytedata length:length]; uiimage *newimage = [uiimage imagewithdata:newdata]; uiimageview *imageview = [[uiimageview alloc] initwithimage:newimage]; imageview.frame = cgrectmake(50, 50, 100, 100); [self.view addsubview:imageview];
Comments
Post a Comment