我想获得一个数据数组,包含存储在iOS(ios8 sdk)上的照片库(ALAsset)中的图片的RGB表示。 我已经尝试过这种方法:从UIImage获取RGB数据的快速方法(照片库)
- 得到ALAsset的一个CGImage与[ALAssetRepresentation fullScreenImage]
- 画CGImage到CGContext上。
这种方法的工作原理,我得到一个指向RGB数据,但这是很慢(有2次转换)。最终目标是在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);
是的我会用后台线程。但另一个问题是内存。我首先从ALAsset(10mb)获取UIImage,然后复制UIImage以获取RGB数据(10mb)。这可能会引发内存压力问题。 – Nitenq 2014-11-12 12:59:44