gpt4 book ai didi

ios - 具有内存映射原始数据的 UIImage (CGImage)

转载 作者:行者123 更新时间:2023-11-29 13:25:28 28 4
gpt4 key购买 nike

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

保罗·哈达德已hinting to something like this我知道这项技术用于 efficient game texture loading with OpenGL .

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

最佳答案

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

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

关于ios - 具有内存映射原始数据的 UIImage (CGImage),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13253057/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com