2012-11-06 75 views
1

有沒有辦法在iOS中的磁盤上存儲和加載原始圖像數據,以便在解壓縮時節省時間和內存並減少應用程序的髒內存佔用?有內存映射原始數據的UIImage(CGImage)

Paul Haddad已經hinting to something like this我明白這項技術用於efficient game texture loading with OpenGL

我的用例,如果爲用戶提供的全屏壁紙,總是顯示在後臺。目前我將它保存到PNG並在應用程序啓動過程中加載它。這浪費了內存和CPU來解壓縮映像。我想保存圖像,然後將其作爲內存映射文件加載。 CoreGraphics/UIKit土地有沒有辦法實現這一點?

回答

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 
} 
+0

這隻能勉強與我的問題讀回。你能解決內存映射部分嗎? – Palimondo