Irrd Irrd - 1 year ago 148
Objective-C Question

How to convert UIImagePicker image into a byte array objective-c

I am selecting a

from image picker. Then I want to pass my selected image as a byte array to the server. so in this delegate I am getting the path of my selected image like this.

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info


Then convert into a byte array I do like this.

NSData *data = [NSData dataWithContentsOfURL:filePath];
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);
NSLog(@"----BITE DATA----%@",byteData);

but this
always get nil. why is that? Please help me.

Answer Source

plz use this

  UIImage *Image = (UIImage*)[info valueForKey:UIImagePickerControllerOriginalImage];
        NSData *imgData = UIImageJPEGRepresentation(Image, 1); //1 it represents the quality of the image.
        NSLog(@"Size of Image(bytes):%d",[imgData length]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download