如何将图像转换为字节并发送到流,然后从输入流接收字节,并将字节再次转换为图像?
答案 0 :(得分:7)
从图像中获取字节:
NSData *imageData = UIImageJPEGRepresentation(image, imageQuality);
从字节中获取图像:
UIImage *image = [UIImage imageWithData: imageData];
BTW,如果需要NSData中的字节,只需调用其bytes方法。
答案 1 :(得分:2)
如何获取流字节并转换回图像,下面是我的代码: 但我得到的图像是空白的。为什么呢?
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
switch (streamEvent) {
case NSStreamEventOpenCompleted:
NSLog(@"Stream opened");
break;
case NSStreamEventHasBytesAvailable:
if (theStream == inputStream) {
uint8_t buffer[5000];
int len;
while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
NSLog(@"len=%d", len);
if (len > 0) {
NSData *pictureData = [NSData dataWithBytes:buffer length:len];
UIImage *images = [[UIImage alloc]initWithData:pictureData];
[imagesview setImage:imagess];
}
}
}
break;
}
}
答案 2 :(得分: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];
答案 3 :(得分: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];
希望你找到解决方案。