带有内存映射原始数据的 UIImage (CGImage) [英] UIImage (CGImage) with memory mapped raw data

查看:28
本文介绍了带有内存映射原始数据的 UIImage (CGImage)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 iOS 的磁盘上存储和加载原始图像数据,以节省解压的时间和内存并减少应用程序的脏内存占用?

Is there a way to store and load raw image data on disk in iOS in order to save time and memory on decompression and reduce app's dirty memory footprint?

Paul Haddad 一直暗示类似这样的事情,我知道这种技术是用于使用OpenGL高效加载游戏纹理.

Paul Haddad has been hinting to something like this and I understand that this technique is used for efficient game texture loading with OpenGL.

我的用例如果用户提供的全屏壁纸总是显示在背景中.目前我将它保存为 PNG 并在应用程序启动期间加载它.这浪费了用于解压缩图像的内存和 CPU.我想保存图像,然后将其作为内存映射文件加载.有没有办法在 CoreGraphics/UIKit 领域实现这一目标?

My use case if for user supplied full screen wallpaper, that is always displayed in the background. Currently I save it to PNG and load it during app startup. This wastes memory and CPU for decompressing the image. I would like to save the image and then later load it as memory mapped file. Is there a way to achieve this in CoreGraphics/UIKit land?

推荐答案

我已经用它来实时保存视频缓冲区的切片

I have used this to save slices of the video buffer in real time

 -(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
}

这篇关于带有内存映射原始数据的 UIImage (CGImage)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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