我一直在尝试实现AVAssetResourceLoaderDelegate,但到目前为止还无法成功设置委托,导致我的委托代码永远不会被调用。
考虑一下:
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
[asset.resourceLoader setDelegate:[[MyAssetResourceLoaderDelegateCode alloc] init]
queue:queue];
NSLog(@"Delegate: %@", asset.resourceLoader.delegate);
此代码始终打印Delegate: (null)
。如何拒绝设置委托时如何设置委托?我错过了什么?
这发生在模拟器和运行iOS 8的iPhone 5上。
答案 0 :(得分:1)
与此对象关联的委托必须采用AVAssetResourceLoaderDelegate协议。 所以代码应该是这样的:
@interface YourClass () <AVAssetResourceLoaderDelegate>
@end
@implementation YourClass
- (void)someMethod
{
NSURL *url = [NSURL URLWithString:@"...."];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
[asset.resourceLoader setDelegate:self queue:queue];
}
#pragma mark - AVAssetResourceLoaderDelegate methods
- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest
{
}
@end
OR
如果你在某个地方有单独的委托MyAssetResourceLoaderDelegateCode,那么你必须保持对你创建的对象MyAssetResourceLoaderDelegateCode的强引用,并将其作为委托传递。当你执行[asset.resourceLoader setDelegate:...时,resourceLoader只是对你所创建的对象有一个弱引用。所以,代码可以是:
@property (nonatomic, strong) MyAssetResourceLoaderDelegateCode *assetObject;
self.assetObject = [MyAssetResourceLoaderDelegateCode new];
[asset.resourceLoader setDelegate:self.assetObject ...];