2011-09-09 59 views
2

我有一個使用的舊代碼,轉換爲石英2D

Rect r;  
GetPortBounds(some_bitmap,&r);  
PixMapHandle somehandle = GetGWorldPixMap(some_bitmap); 
if(LockPixels(somehandle)){ 
    TPixel *data = (TPixel *) GetPixBaseAddr(somehandle); 
    long row_bytes = GetPixRowBytes(somehandle); 
    // doing something 
    UnlockPixels(somehandle); 
} 

誰能幫我在石英2D替換代碼

回答

1

要修改一個位圖與石英您可以初始化CGContextRef與圖像和CGContextDraw...例程繪製到該上下文。
(我寫了下面的示例代碼的的NSView子類。這有點低效的。如果你使用的代碼,分開就可以在高德保持周圍的東西。)

- (void)drawRect:(NSRect)dirtyRect 
{ 
    //Load an image ... 
    NSImage* image = [[NSImage alloc] initWithContentsOfFile:@"/Library/Desktop Pictures/Grass Blades.jpg"]; 
    CGImageRef testImage = [[[image representations] objectAtIndex:0] CGImage]; 
    [image release]; 
    CGDataProviderRef dataProvider = CGImageGetDataProvider(testImage); 
    //... and retrieve its pixel data 
    CFDataRef imageData = CGDataProviderCopyData(dataProvider); 
    void* pixels = (void*)CFDataGetBytePtr(imageData); 
    CGColorSpaceRef colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); 
    //Init a quartz context that uses the pixel data memory as buffer 
    CGContextRef drawContext = CGBitmapContextCreate(pixels, CGImageGetWidth(testImage), CGImageGetHeight(testImage), CGImageGetBitsPerComponent(testImage), CGImageGetBytesPerRow(testImage), colorspace, CGImageGetBitmapInfo(testImage)); 
    CGContextSetRGBFillColor(drawContext, 0.8, 0.8, 0.8, 1.0); 
    //Do something with the newly created context 
    CGContextFillRect(drawContext, CGRectMake(20.0, 20.0, 200.0, 200.0));  
    CGColorSpaceRelease(colorspace); 
    CGImageRef finalImage = CGBitmapContextCreateImage(drawContext); 
    //Draw the modified image to the screen 
    CGContextDrawImage([[NSGraphicsContext currentContext] graphicsPort], dirtyRect, finalImage); 
    CFRelease(imageData); 
    CGImageRelease(finalImage); 
    CGContextRelease(drawContext); 
} 
+0

實際的代碼是有點複雜領先,有沒有什麼辦法可以在不使用不推薦使用的函數的情況下獲取pixmaphandle? – Peter

+0

實際上前面的代碼有點複雜,有什麼方法可以在不使用不推薦使用的函數的情況下獲取pixmaphandle嗎? – Peter

+0

我不知道有一種方法來檢索未被棄用的pixmaphandle。上面的代碼應該可以工作,並且是Quartz,因此也是未來的證明。 –