2011-08-12 3 views
10

Próbuję utworzyć bitmapę w pamięci jako część funkcji wzorca, którą wywoła metoda drawLayer: inContext: (ta metoda jest częścią protokołu delegata CALayer). Funkcja wzór wygląda podobnie do tego:Jak wygląda prawidłowe wywołanie funkcji CGImageCreate, jeśli dostawca danych używa tablicy utworzonej przez aplikację?

static const size_t kComponentsPerPixel = 4; 
static const size_t kBitsPerComponent = sizeof(unsigned char) * 8; 

NSInteger layerHeight = 160; 
NSInteger layerWidth = 160; 
CGContextSaveGState(context); 

CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); 

size_t bufferLength = layerWidth * layerHeight * kComponentsPerPixel; 

unsigned char *buffer = malloc(bufferLength); 

// The real function does something more interesting with the buffer, but I cut it 
// to reduce the complexity while I figure out the crash. 
for (NSInteger i = 0; i < bufferLength; ++i) 
{ 
    buffer[i] = 255; 
} 
//memset(buffer, 255, bufferLength); 

CGDataProviderRef provider = 
CGDataProviderCreateWithData(NULL, &buffer, bufferLength, NULL);//freeBitmapBuffer); 

CGImageRef imageRef = 
CGImageCreate(layerWidth, layerHeight, kBitsPerComponent, 
       kBitsPerComponent * kComponentsPerPixel, 
       kComponentsPerPixel * layerWidth, 
       rgb, 
       kCGBitmapByteOrderDefault | kCGImageAlphaLast, 
       provider, NULL, false, kCGRenderingIntentDefault); 

CGContextDrawImage(context, CGRectMake(0, 0, 160, 160), imageRef); 

CGImageRelease(imageRef); 
CGDataProviderRelease(provider); 
CGColorSpaceRelease(rgb);  

CGContextRestoreGState(context); 

Później, gdy drawLayer: InContext: nazywa CGContextFillRect aby wyświetlić wzór tworzony przez tę funkcję, otrzymuję EXC_BAD_ACCESS. Wierzchołek stosu to CGSConvertAlphaByte. W tym momencie przyjrzałem się pamięci bufora i wydawało mi się, że wszystko jest w porządku - ustawione dokładnie na to, do czego zostało ustawione, gdy wywołano funkcję wzorca.

Myślę, że być może pomógł mi jakiś parametr CGImageCreate, prawdopodobnie flagi. Lub bufor nie jest wypełniony we właściwej kolejności, ale nie jestem pewien, jak mogę się pomylić, jeśli każdy bajt zostanie wypełniony tą samą wartością.

Wszelkie pomysły lub przykłady podobnego kodu, który nie ulega awarii?

Odpowiedz

4

OK, więc fora deweloperów Apple zauważyły ​​błąd: przechodziłem z jakiegoś powodu z bufora z jakiegoś powodu zamiast z bufora.

Powiązane problemy