从UIImage获取RGB数据的快速方法(照片库) [英] fast method to get RGB data from UIImage (photo library)

查看:398
本文介绍了从UIImage获取RGB数据的快速方法(照片库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取一个数据数组,其中包含存储在iOS(ios8 sdk)上的图片库(ALAset)中的图片的RGB表示形式. 我已经尝试过这种方法:

I would like to get a data array containing the RGB representation of a picture stored in the photo library (an ALAsset) on iOS (ios8 sdk). I already tried this method :

  • 使用[ALAssetRepresentation fullScreenImage]从ALAsset获取CGImage
  • 将CGImage绘制到CGContext.

该方法有效,我得到了一个指向rgb数据的指针,但这确实很慢(有2次转换).最终目标是将图像快速加载到OpenGL纹理中.

That method works, I get a pointer to rgb data, but this is really slow (there are 2 conversions). The final goal is to load the image quickly in a OpenGL texture.

我的代码从照片库获取图像

My code to get an image from Photo library

ALAsset* currentPhotoAsset = (ALAsset*) [self.photoAssetList objectAtIndex:_currentPhotoAssetIndex];
ALAssetRepresentation *representation = [currentPhotoAsset defaultRepresentation];
//-> REALLY SLOW
UIImage *currentPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];

我要在CGContext上绘制的代码:

My code to draw on the CGContext :

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * textureWidth;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(textureData, textureWidth, textureHeight,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGColorSpaceRelease(colorSpace);
//--> THAT'S REALLY SLOW
CGContextDrawImage(context, CGRectMake(0, 0, textureWidth, textureHeight), cgimage);
CGContextRelease(context);

推荐答案

您无能为力,但是如果您找到一种方法,我很乐意听到.

There is not much you can do but I if you find a way I would be happy to hear about it.

问题是您需要解压缩图像(jpg,png ...),这通常是通过创建CGImage(UIImage只是围绕它的包装器)来完成的.但是,然后您将不允许直接从CGImage获取数据指针,而是需要复制它们(真正缓慢的绘制调用).但是,如果目标大小和格式与源相同,那么此操作应该很快,因为应该或多或少地简单地复制数据.另一方面,如果您的textureWidthtextureHeight不同,则需要对这些像素的图像尺寸进行插值,并且此功能甚至会变慢几倍.

The thing is you need to decompress the image (jpg, png...) which is usually done by creating a CGImage (UIImage is just a wrapper around it). But then you are not allowed to get the data pointer directly from the CGImage but you need to copy them (the really slow draw call). Though then again if the target size and format are the same as the source this operation should be quite fast since the data should more or less simply be copied. On the other hand if your textureWidth and textureHeight are different then the image dimensions those pixels need to be interpolated and this function can become even a few times slower.

我看到的唯一方法是获得一些库,以从文件中直接解压缩图像并获取该图像的数据指针.但是我从来没有加载图像纹理(使用背景线程)的性能问题.

The only way out of this I see is to get some library to directly decompress the image from file and get the data pointer of that image. But I never had a performance issue for loading image textures (use a background thread).

无论如何,如果您尚未执行类似的操作来获取图像大小,则找到可以填充图像大小的POT(2的幂)宽度和高度.然后,我使用这些POT尺寸创建一个空纹理,并调用子图像以将原始图像数据传递给该纹理.我使用一个自定义纹理类来处理它,该类还包含(生成)纹理坐标,因此纹理的正确部分被绘制到帧缓冲区.然后,扩展该类以支持图集,这通常是您在处理许多图像(纹理)时要执行的操作.

Anyway if you are not doing something similar already how I use this is to get the image size, then find the POT (power of two) width and height that fills the image size. Then I create an empty texture with those POT dimensions and call sub image to pass the original image data to the texture. I use a custom texture class to handle this which also contains (generates) texture coordinates so the correct part of the texture is drawn to the frame buffer. Then this class is extended to support atlasing which is generally what you want to do when dealing with many images (textures).

我希望此信息能以任何方式为您提供帮助...

I hope this info helps you in any way...

这篇关于从UIImage获取RGB数据的快速方法(照片库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆