Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从UIImage(照片库)获取RGB数据的快速方法_Ios_Objective C_Opengl Es_Uiimage_Alasset - Fatal编程技术网

Ios 从UIImage(照片库)获取RGB数据的快速方法

Ios 从UIImage(照片库)获取RGB数据的快速方法,ios,objective-c,opengl-es,uiimage,alasset,Ios,Objective C,Opengl Es,Uiimage,Alasset,我想获得一个数据数组,其中包含存储在iOS(IOS8SDK)上的照片库(ALASET)中的图片的RGB表示形式。 我已经尝试过这种方法: 使用[ALAssetRepresentation fullScreenImage]从ALAsset获取CGA图像 将CGImage绘制到CGContext 这种方法很有效,我得到了一个指向rgb数据的指针,但是这非常慢(有两个转换)。最终目标是在OpenGL纹理中快速加载图像 从照片库获取图像的代码 ALAsset* currentPhotoAsset =

我想获得一个数据数组,其中包含存储在iOS(IOS8SDK)上的照片库(ALASET)中的图片的RGB表示形式。 我已经尝试过这种方法:

  • 使用[ALAssetRepresentation fullScreenImage]从ALAsset获取CGA图像
  • 将CGImage绘制到CGContext
这种方法很有效,我得到了一个指向rgb数据的指针,但是这非常慢(有两个转换)。最终目标是在OpenGL纹理中快速加载图像

从照片库获取图像的代码

ALAsset* currentPhotoAsset = (ALAsset*) [self.photoAssetList objectAtIndex:_currentPhotoAssetIndex];
ALAssetRepresentation *representation = [currentPhotoAsset defaultRepresentation];
//-> REALLY SLOW
UIImage *currentPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];
我要在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);

你能做的不多,但如果你能找到办法,我会很高兴听到

问题是您需要解压缩图像(jpg、png…),这通常是通过创建
CGImage
UIImage
只是一个包装器)。但是不允许直接从
CGImage
获取数据指针,但是需要复制它们(非常慢的draw调用)。尽管如此,如果目标大小和格式与源相同,此操作应该相当快,因为数据应该或多或少地被复制。另一方面,如果您的
纹理宽度
纹理高度
不同,则需要对这些像素的图像尺寸进行插值,并且此函数可能会变慢几倍

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

无论如何,如果你还没有做类似的事情,那么我用它来获得图像大小,然后找到填充图像大小的POT(二的幂)宽度和高度。然后,我创建一个具有这些POT维度的空纹理,并调用sub-image将原始图像数据传递给纹理。我使用一个自定义纹理类来处理这个问题,它还包含(生成)纹理坐标,以便将纹理的正确部分绘制到帧缓冲区。然后,该类被扩展以支持atlasing,这通常是处理许多图像(纹理)时要执行的操作


我希望这些信息能对您有所帮助……

是的,我将使用背景线程。但另一个问题是内存。我首先从ALAsset(10mb)获取UIImage,然后复制UIImage以获取RGB数据(10mb)。这可能会引发内存压力问题。