2

today my task is convert byte array to image

First I try to convert image to byte array :-

For converting Image to Byte array first we have to do is to convert that particular image [UIImage] to NSData.Then we will convert that NSData to Byte array. Here I will give the sample code, just go through...

//Converting UIImage to NSData
    UIImage *image = [UIImage imageNamed: @"photo-04.jpg"];

    NSData *imageData = UIImagePNGRepresentation(image);

    //Converting NSData to Byte array
    NSUInteger len = [imageData length];
    NSLog(@"Byte Lendata1  %lu",(unsigned long)len);

    Byte *byteData = (Byte*)malloc(len);
    memcpy(byteData, [imageData bytes], len);

i am try like this Convert byte to imageView

 const unsigned char *bytes = [imageData bytes];

    NSUInteger length = [imageData length];

    NSMutableArray *byteArray = [NSMutableArray array];

    for (NSUInteger i = 0; i < 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 to byte array but i want convert byte array to image so please help me thanks in advanced.

PavanAlapati
  • 41
  • 2
  • 8
  • just do the opposite. Show what you have tried. – rmaddy Mar 11 '14 at 04:47
  • @rmaddy thanks for replay i tried like this NSData *imageData1 = [NSData dataWithBytes:byteData length:len]; UIImage *image1 = [UIImage imageWithData:imageData1]; UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)]; imgView.image=image1; – PavanAlapati Mar 11 '14 at 05:45
  • Don't put your code in a comment. Update your question with your code. – rmaddy Mar 11 '14 at 05:46
  • @rmaddy add my code in question please help me – PavanAlapati Mar 11 '14 at 06:03
  • @HimanshuJoshi I have "photo-04.jpg" image in my app,i have converted the image into NSData and to byte data.And after that i have again converted back into NSData from byte data.I want to display my image in UIImageView from this NSData value.But it's not displayed. plz suggest me how to display. – PavanAlapati Mar 11 '14 at 07:09
  • does the @kukushi patch did not worked? – Himanshu Joshi Mar 11 '14 at 07:12
  • @HimanshuJoshi i tried but image is not displayed – PavanAlapati Mar 11 '14 at 07:16

2 Answers2

5

First, you need to convert bytes to NSData

NSData *imageData = [NSData dataWithBytes:bytesData length:length];

Then, convert the data back to image.

UIImage *image = [UIImage imageWithData:imageData];

And I suggest you should first searching about the documentations when problems occur.

Here is all:

UIImage *image = [UIImage imageNamed:@"RAC.png"];

NSData *imageData = UIImagePNGRepresentation(image);
// UIImageJPGRepresentation also 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];
kukushi
  • 647
  • 9
  • 22
  • @PavanAlapati Did you forget to add the imageView to the self.view as the subview? My answer work fine for me. – kukushi Mar 11 '14 at 11:22
1

You are representing the data with wrong format

Your image is of format jpg and you are representing with PNG data.

For jpg or jpeg format you should use UIImageJPEGRepresentation

NSData * UIImageJPEGRepresentation (
   UIImage *image,
   CGFloat compressionQuality
);

The required statement will be

NSData *imageData = UIImageJPEGRepresentation(image, 0.0f);// Set Compression quality to 0.0. You can change it.

For png format you should use UIImagePNGRepresentation

NSData * UIImagePNGRepresentation (
   UIImage *image
);

The required statement will be

NSData *imageData = UIImagePNGRepresentation(image);

To convert the NSData back to UIImage, use

UIImage *image = [UIImage imageWithData:imageData];

READ THE APPLE DOCS. SEE UNDER IMAGE MANIPULATION

Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32