Irrd Irrd - 3 months ago 35
Objective-C Question

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

I am selecting a

UIImage
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

filePath=info[UIImagePickerControllerReferenceURL];


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
data
always get nil. why is that? Please help me.
Thanks

Answer

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]);