【问题标题】:How to save and read a CGLayerRef, in a NSmutableArray如何在 NSmutableArray 中保存和读取 CGLayerRef
【发布时间】:2009-12-01 18:22:45
【问题描述】:

我对 CGLayerRef “元素”有疑问(因为它们不是对象!)

在我的应用程序中,我想存储从位图创建的 CGLayerRef 列表,以便在我的 DrawRect 上获得良好的帧速率。

如何在这段代码中存储和读取 CGLayerRef ?

这是存储在 NSMutableArray 中的代码

    CGLayerRef imageLayer;
// create my layer...
// init  my NSMutableArray
if (layerTable == nil) {
    layerTable = [[NSMutableArray alloc] init];
}
// and store my CGCLayer in this array with NSValue
[layerTable addObject:[[NSValue alloc] initWithBytes:imageLayer objCType:"CGLayerRef"]];

现在是我认为 DrawRect 的代码:

“actualIdp”是一个int,用于标识要绘制的图层。

-(void)drawInContext:(CGContextRef)context{
// other code...

// here I want to read my CGLayerRef


//test with NSValue, but i don't know NSValue

NSValue *val = [layerTable objectAtIndex:actualIdp];
CGLayerRef layerToShow;
[val getValue:layerToShow]; // maybe an error

CGContextDrawLayerInRect(context, imageRect, layerToShow );

}

这里是更改我的 id 的代码:

-(void)showThisId:(int)idp{
actualIdp = idp;
[self setNeedsDisplay];

};

【问题讨论】:

    标签: iphone view quartz-graphics


    【解决方案1】:

    你遗漏了@encode 指令:

    [layerTable addObject:[[NSValue alloc] initWithBytes:imageLayer objCType:"CGLayerRef"]];
    

    应改为:

    [layerTable addObject:[[NSValue alloc] initWithBytes:imageLayer objCType:@encode(CGLayerRef)]];
    

    您需要将缓冲区的地址传递给[NSValue getValue:]。所以这一行:

    [val getValue:layerToShow];
    

    应该是:

    [val getValue:&layerToShow];
    

    我认为这应该可行。

    【讨论】:

    • 这个答案很好,但为了最好地使用我的 CGCLayerRef 值,我写了这一行:[layerTable addObject:[[NSValue alloc] initWithBytes:&imageLayer objCType:@encode(CGLayerRef)]];
    猜你喜欢
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    相关资源
    最近更新 更多