1

I am converting UIImageinto byte like this

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

when I print this byteData in log I see a value like this.

(lldb) po byteData
"\377\330\377\340

But how can I make a byte array from this? Please help me. Thanks

UPDATE

this is how our web application pass data to the same service.

byte[] fileContent; 
using (var inputStream = file.InputStream)
 { var memoryStream = inputStream as MemoryStream;
   if (memoryStream == null) { memoryStream = new MemoryStream();
   inputStream.CopyTo(memoryStream); } fileContent = memoryStream.ToArray(); 
 }

So I want to do the same in my iOS application.

KSR
  • 1,699
  • 15
  • 22
Irrd
  • 325
  • 1
  • 6
  • 18

3 Answers3

0

You have to convert your UIImage into NSData like this......

UIImage *imgObj = [UIImage imageNamed:@"image.jpg"];

NSData *imgData = UIImageJPEGRepresentation(imgObj, 0.9);
Community
  • 1
  • 1
Abha
  • 1,032
  • 1
  • 13
  • 36
0

May be below code could help:

@try {
    NSData *data = UIImagePNGRepresentation(img);
    NSUInteger len = data.length;
    uint8_t bytes = (uint8_t )[data bytes];
    NSMutableString *result = [NSMutableString stringWithCapacity:len];
    for (NSUInteger i = 0; i < len; i++) {
        if (i) {
            [result appendString:@","];
        }
        [result appendFormat:@"%d",bytes];
    }
    return result;
} @catch (NSException * e) {
    NSLog(@"Exception: %@", e);
}
@finally {
    NSLog(@"finally");
}
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
0

This is the simplest way...

UIImage *img =  [UIImage imageNamed:@"yourimage.png"];

NSData *imageData = UIImagePNGRepresentation(img);

or

UIImage *img =  [UIImage imageNamed:@"yourimage.jpg"];

CGFloat quality = 0.80;

NSData *imageData = UIImageJPEGRepresentation(img,quality);
milos.ai
  • 3,882
  • 7
  • 31
  • 33