Simeon 的回答有效,但改变了应用程序中所有基于 EAGL 的视图的行为。我有一些需要强制支持的视图,而另一些则不需要,所以我通过创建 GLKView 和 CEAGLLayer 的子类提出了一个稍微不同的解决方案,如下所示:
@interface RetainedEAGLLayer : CAEAGLLayer
@end
@implementation RetainedEAGLLayer
- (void)setDrawableProperties:(NSDictionary *)drawableProperties {
// Copy the dictionary and add/modify the retained property
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] initWithCapacity:drawableProperties.count + 1];
[drawableProperties enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
// Copy all keys except the retained backing
if (![key isKindOfClass:[NSString class]]
|| ![(NSString *)key isEqualToString:kEAGLDrawablePropertyRetainedBacking])
[mutableDictionary setObject:object forKey:key];
}];
// Add the retained backing setting
[mutableDictionary setObject:@(YES) forKey:kEAGLDrawablePropertyRetainedBacking];
// Continue
[super setDrawableProperties:mutableDictionary];
[mutableDictionary release];
}
@end
和这个
@interface RetainedGLKView : GLKView
@end
@implementation RetainedGLKView
+ (Class)layerClass {
return [RetainedEAGLLayer class];
}
@end
现在,对于那些我想强制保留支持的视图,我可以只使用 RetainedGLKView 而不是 GLKView。