質問

私は使用する古いコードを持っています、

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);  
}  

誰でもQuartz2Dの交換コードを手伝ってもらえますか

役に立ちましたか?

解決

Quartzを使用してビットマップを変更するには、画像を使用してCGContExtrefを初期化し、そのコンテキストに描画できます。 CGContextDraw... ルーチン。
(NSViewサブクラスの次のサンプルコードを書きました。それは少し非効率的です。コードを使用する場合は、ivarsで保持できるものを分離します。)

- (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);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top