UIImage字节数组
答
您可以将UIImage转换为NSData对象,然后从那里提取字节数组。下面是一些示例代码:
UIImage *image = [UIImage imageNamed:@"image.png"];
NSString *byteArray = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
如果使用一个PNG图像可以使用UIImagePNGRepresentation功能如上图所示,或者如果使用的是JPEG图像,则可以使用UIImageJPEGRepresentation功能。文档可以使用UIImage Class Reference
答
NSData *data = UIImagePNGRepresentation(image);
可以图像转换成数据,现在通过使用getBytes:length:
或getBytes:range:
答
下面是一个简单的功能,适用于iOS从UIImage的转换为unsigned char *字节数组转换DAT到字节上 - >
+ (unsigned char*)UIImageToByteArray:(UIImage*)image; {
unsigned char *imageData = (unsigned char*)(malloc(4*image.size.width*image.size.height));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGImageRef imageRef = [image CGImage];
CGContextRef bitmap = CGBitmapContextCreate(imageData,
image.size.width,
image.size.height,
8,
image.size.width*4,
colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(bitmap, CGRectMake(0, 0, image.size.width, image.size.height), imageRef);
CGContextRelease(bitmap);
CGColorSpaceRelease(colorSpace);
return imageData;
}
这对我很有帮助。谢谢你,苏哈尔你的帖子。 – Patricia