有没有办法在iOS上的磁盘上存储和加载原始图像数据,以节省解压缩的时间和内存,减少应用程序的脏内存占用?
Paul Haddad一直hinting to something like this,我知道这项技术用于efficient game texture loading with OpenGL。
我的用例如果是用户提供的全屏壁纸,则始终显示在后台。目前我将其保存到PNG并在应用启动期间加载它。这会浪费内存和CPU来解压缩图像。我想保存图像,然后将其加载为内存映射文件。有没有办法在CoreGraphics / UIKit土地上实现这一目标?
答案 0 :(得分:1)
我用它来实时保存视频缓冲区的片段
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
size_t sliceSize = bytesPerRow*sliceWidth*sizeof(char);
int slicePos= bytesPerRow*sliceStart*sizeof(char);
NSData *rawFrame = [NSData dataWithBytes:(void*)baseAddress+slicePos length:sliceSize];
inputPath = [NSString stringWithFormat:@"%@%@%d", NSTemporaryDirectory(), @"slice",step];
[[NSData dataWithData:rawFrame] writeToFile:inputPath atomically:NO];
}
然后我用
回读-(void)readSlice
{
//read slice i
NSString *filePath = [NSString stringWithFormat:@"%@%@%d", NSTemporaryDirectory(), @"slice",i];
char* imgD= (char*)malloc(sliceSize);
[[NSData dataWithContentsOfFile:filePath] getBytes:imgD];
CGContextRef context = CGBitmapContextCreate(imgD, sliceHeight,sliceWidth, 8, bytesPerRad, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef quartzImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
free(imgD);
//do something with quartzImage
}