Совместное использование памяти между CGImageRef и UIImage - PullRequest
2 голосов
/ 03 октября 2009

Есть ли способ создать объект UIImage из неэкранной поверхности без копирования данных основного пикселя?

Я бы хотел сделать что-то вроде этого:

// These numbers are made up obviously...
CGContextRef offscreenContext = MyCreateBitmapContext(width, height);

// Draw into the offscreen buffer
// Draw commands not relevant...

// Convert offscreen into CGImage
// This consumes ~15MB
CGImageRef offscreenContextImage = CGBitmapContextCreateImage(offscreenContext);

// This allocates another ~15MB
// Is there any way to share the bits from the 
// CGImageRef instead of copying the data???
UIImage * newImage = [[UIImage alloc] initWithCGImage:offscreenContextImage];

// Releases the original 15MB, but the spike of 30MB total kills the app.
CGImageRelease(offscreenContextImage);
CGContextRelease(offscreenContext);

Память высвобождается и выравнивается до приемлемого размера, но всплеск памяти в 30 МБ убивает приложение. Есть ли способ поделиться данными пикселей?

Я подумал о том, чтобы сохранить внеэкранный буфер в файл и снова загрузить данные, но это взлом, и для удобства iPhone требуются UIImage для его сохранения ...

Ответы [ 2 ]

0 голосов
/ 03 октября 2009

Вы можете попробовать освободить контекст сразу после создания CGImage, освободив память, используемую контекстом, потому что CGBitmapContextCreateImage () создает копию контекста.

Как это:

CGImageRef offscreenContextImage = CGBitmapContextCreateImage(offscreenContext);

CGContextRelease(offscreenContext);

UIImage * newImage = [[UIImage alloc] initWithCGImage:offscreenContextImage];

// ...

CGImageRelease(offscreenContextImage);
0 голосов
/ 03 октября 2009

Может

UIImage *newImage = [[UIImage alloc[ initWithData:offScreenContext];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...