3

How to convert images to bytes and send out to a stream, then receive bytes from the input stream, and convert the bytes into images again?

4

3 回答 3

7

For getting bytes from an image:

NSData *imageData = UIImageJPEGRepresentation(image, imageQuality);

For getting an image from bytes:

UIImage *image = [UIImage imageWithData: imageData];

BTW, If you need the bytes from the NSData just call its bytes method.

于 2012-09-04T06:07:01.880 回答
1

将图像转换为字节,如下所示:

NSData *imageData = UIImagePNGRepresentation(yourImage.image);  
NSUInteger len = [imageData length];
Byte *byteData= (Byte*)malloc(len);
[imageData  getBytes:byteData length:len];

现在像这样字节成图像:

NSData *pictureData = [NSData dataWithBytes:byteData];
UIImage *image = [[UIImage alloc]initWithData:pictureData];
于 2012-09-04T06:09:28.970 回答
1

UIImage到字节数组

UIImage *image = [UIImage imageNamed:@"image.png"];
NSData *data = UIImagePNGRepresentation(image);
NSString *byteArray  = [data base64Encoding];

字节数组到UIImage

+ (UIImage *)imageWithData:(NSData *)data;
NSData *data = [NSData dataWithBytes:YOUR_BYTE_ARRAY length:ARRAY_LENGTH];
UIImage *img = [UIImage imageWithData:data];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];

希望您找到解决方案。

于 2012-09-04T06:18:39.650 回答