2011-09-09 2 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); 
} 

사람이

답변

1

당신이 CGContextRef 초기화 할 수 있습니다 석영과 비트 맵을 수정하려면 수정 차원에서 대체 코드를 좀 도와 줄래 그 이미지와 그 문맥을 CGContextDraw... 루틴으로 그립니다.
는 (I는 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

실제로 코드는 앞서 약간 복잡입니다 , 비 deprecated 함수를 사용하지 않고 pixmaphandle을 얻을 수있는 방법이 있습니까? – Peter

+0

실제로 코드는 약간 복잡합니다. 비 deprecated 함수를 사용하지 않고 pixmaphandle을 얻을 수있는 방법이 있습니까? – Peter

+0

나는 deprecated되지 않는 pixmaphandle을 검색하는 방법을 알지 못합니다. 위의 코드는 작동해야하며 Quartz 전용이므로 향후 증명됩니다. –