首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从quickdraw到quartz 2D的转换

从quickdraw到quartz 2D的转换
EN

Stack Overflow用户
提问于 2011-09-09 17:41:50
回答 1查看 750关注 0票数 2

我有一个旧代码,它使用,

代码语言:javascript
复制
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);  
}  

有人能帮我解决quartz 2d中的替换代码吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-09 20:55:02

要使用Quartz修改位图,可以使用图像初始化CGContextRef并使用CGContextDraw...例程绘制到该上下文。

(我为NSView子类编写了以下示例代码。这有点低效。如果您使用代码,请将您可以保留在iVars中的内容分开。)

代码语言:javascript
复制
- (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);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7359749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档