fixed problem with fetchData problem and updated nic cage example

This commit is contained in:
Luke Parham 2015-12-18 03:41:53 -06:00
parent a75871055f
commit 616e9ffbcb
3 changed files with 56 additions and 40 deletions

View File

@ -1,13 +1,6 @@
#import "ASVideoNode.h"
#import "ASDisplayNodeInternal.h"
#import "ASDisplayNode+Subclasses.h"
#import "ASDisplayNode+FrameworkPrivate.h"
@interface ASDisplayNode ()
- (void)setInterfaceState:(ASInterfaceState)newState;
@end
@interface ASVideoNode () {
ASDN::RecursiveMutex _lock;
@ -24,13 +17,16 @@
@end
@interface ASDisplayNode ()
- (void)setInterfaceState:(ASInterfaceState)newState;
@end
@implementation ASVideoNode
- (instancetype)init {
if (!(self = [super init])) { return nil; }
_playerNode = [[ASDisplayNode alloc] initWithLayerBlock:^CALayer *{ return [[AVPlayerLayer alloc] init]; }];
[self addSubnode:_playerNode];
self.gravity = ASVideoGravityResizeAspect;
@ -114,7 +110,7 @@
- (void)fetchData
{
[super fetchData];
@try {
[_currentItem removeObserver:self forKeyPath:NSStringFromSelector(@selector(status))];
}
@ -124,18 +120,46 @@
{
ASDN::MutexLocker l(_lock);
_currentItem = [[AVPlayerItem alloc] initWithAsset:_asset];
[_currentItem addObserver:self forKeyPath:NSStringFromSelector(@selector(status)) options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:NULL];
[((AVPlayerLayer *)_playerNode.layer).player replaceCurrentItemWithPlayerItem:_currentItem];
if (((AVPlayerLayer *)_playerNode.layer).player) {
[((AVPlayerLayer *)_playerNode.layer).player replaceCurrentItemWithPlayerItem:_currentItem];
} else {
((AVPlayerLayer *)_playerNode.layer).player = [[AVPlayer alloc] initWithPlayerItem:_currentItem];
}
}
if (_shouldAutoPlay) {
[self play];
}
}
//- (void)fetchData
//{
// [super fetchData];
//
// @try {
// [_currentItem removeObserver:self forKeyPath:NSStringFromSelector(@selector(status))];
// }
// @catch (NSException * __unused exception) {
// NSLog(@"unnecessary removal in fetch data");
// }
//
// {
// ASDN::MutexLocker l(_lock);
//
// _currentItem = [[AVPlayerItem alloc] initWithAsset:_asset];
// [_currentItem addObserver:self forKeyPath:NSStringFromSelector(@selector(status)) options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:NULL];
//
// [((AVPlayerLayer *)_playerNode.layer).player replaceCurrentItemWithPlayerItem:_currentItem];
// }
//
// if (_shouldAutoPlay) {
// [self play];
// }
//}
- (void)clearFetchedData
{
[super clearFetchedData];
@ -150,7 +174,7 @@
- (void)setPlayButton:(ASButtonNode *)playButton
{
ASDN::MutexLocker l(_lock);
_playButton = playButton;
[self addSubnode:playButton];
@ -161,7 +185,7 @@
- (ASButtonNode *)playButton
{
ASDN::MutexLocker l(_lock);
return _playButton;
}
@ -188,7 +212,7 @@
- (void)setGravity:(ASVideoGravity)gravity {
ASDN::MutexLocker l(_lock);
switch (gravity) {
case ASVideoGravityResize:
((AVPlayerLayer *)_playerNode.layer).videoGravity = AVLayerVideoGravityResize;
@ -208,7 +232,7 @@
- (ASVideoGravity)gravity;
{
ASDN::MutexLocker l(_lock);
if ([((AVPlayerLayer *)_playerNode.layer).contentsGravity isEqualToString:AVLayerVideoGravityResize]) {
return ASVideoGravityResize;
}
@ -222,7 +246,7 @@
- (void)play;
{
ASDN::MutexLocker l(_lock);
[[((AVPlayerLayer *)_playerNode.layer) player] play];
_shouldBePlaying = YES;
_playButton.alpha = 0.0;
@ -231,7 +255,7 @@
UIActivityIndicatorView *spinnnerView = [[UIActivityIndicatorView alloc] initWithFrame:_playButton.frame];
spinnnerView.color = [UIColor whiteColor];
[spinnnerView startAnimating];
return spinnnerView;
}];
@ -246,8 +270,8 @@
- (void)pause;
{
// ASDN::MutexLocker l(_lock);
ASDN::MutexLocker l(_lock);
[[((AVPlayerLayer *)_playerNode.layer) player] pause];
_shouldBePlaying = NO;
_playButton.alpha = 1.0;
@ -265,3 +289,4 @@
}
@end

View File

@ -48,15 +48,8 @@ static NSString *kLinkAttributeName = @"PlaceKittenNodeLinkAttributeName";
_textNode.linkAttributeNames = @[ kLinkAttributeName ];
// generate an attributed string using the custom link attribute specified above
NSString *blurb = @"kittens courtesy placekitten.com \U0001F638";
NSString *blurb = @"Nic Cage courtesy of himself.";
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:blurb];
[string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Light" size:16.0f] range:NSMakeRange(0, blurb.length)];
[string addAttributes:@{
kLinkAttributeName: [NSURL URLWithString:@"http://placekitten.com/"],
NSForegroundColorAttributeName: [UIColor grayColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle | NSUnderlinePatternDot),
}
range:[blurb rangeOfString:@"placekitten.com"]];
_textNode.attributedString = string;
// add it as a subnode, and we're done

View File

@ -18,9 +18,9 @@
#import "NicCageNode.h"
static const NSInteger kLitterSize = 20; // intial number of kitten cells in ASTableView
static const NSInteger kLitterBatchSize = 10; // number of kitten cells to add to ASTableView
static const NSInteger kMaxLitterSize = 100; // max number of kitten cells allowed in ASTableView
static const NSInteger kCageSize = 20; // intial number of Cage cells in ASTableView
static const NSInteger kCageBatchSize = 10; // number of Cage cells to add to ASTableView
static const NSInteger kMaxCageSize = 100; // max number of Cage cells allowed in ASTableView
@interface ViewController () <ASTableViewDataSource, ASTableViewDelegate>
{
@ -55,11 +55,11 @@ static const NSInteger kMaxLitterSize = 100; // max number of kitten cell
_tableView.asyncDelegate = self;
// populate our "data source" with some random kittens
_kittenDataSource = [self createLitterWithSize:kLitterSize];
_kittenDataSource = [self createLitterWithSize:kCageSize];
_blurbNodeIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
self.title = @"Kittens";
self.title = @"Nic Cage";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self
action:@selector(toggleEditingMode)];
@ -69,18 +69,16 @@ static const NSInteger kMaxLitterSize = 100; // max number of kitten cell
- (NSMutableArray *)createLitterWithSize:(NSInteger)litterSize
{
NSMutableArray *kittens = [NSMutableArray arrayWithCapacity:litterSize];
NSMutableArray *cages = [NSMutableArray arrayWithCapacity:litterSize];
for (NSInteger i = 0; i < litterSize; i++) {
// placekitten.com will return the same kitten picture if the same pixel height & width are requested,
// so generate kittens with different width & height values.
u_int32_t deltaX = arc4random_uniform(10) - 5;
u_int32_t deltaY = arc4random_uniform(10) - 5;
CGSize size = CGSizeMake(350 + 2 * deltaX, 350 + 4 * deltaY);
[kittens addObject:[NSValue valueWithCGSize:size]];
[cages addObject:[NSValue valueWithCGSize:size]];
}
return kittens;
return cages;
}
- (void)setKittenDataSource:(NSMutableArray *)kittenDataSource {
@ -160,7 +158,7 @@ static const NSInteger kMaxLitterSize = 100; // max number of kitten cell
- (BOOL)shouldBatchFetchForTableView:(UITableView *)tableView
{
return _kittenDataSource.count < kMaxLitterSize;
return _kittenDataSource.count < kMaxCageSize;
}
- (void)tableView:(UITableView *)tableView willBeginBatchFetchWithContext:(ASBatchContext *)context
@ -170,7 +168,7 @@ static const NSInteger kMaxLitterSize = 100; // max number of kitten cell
dispatch_async(dispatch_get_main_queue(), ^{
// populate a new array of random-sized kittens
NSArray *moarKittens = [self createLitterWithSize:kLitterBatchSize];
NSArray *moarKittens = [self createLitterWithSize:kCageBatchSize];
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];