osx - Animating and removing an NSImageView object failed? -
my app situation:
having app displaying dynamic picture. , want capture current image , save disc.
my goal:
working on animated visual effect: add image view on top, , animates origin frame cgrecrzero. finally, remove image view vie hierarchy after animation done.
now problem:
first time triggerring action, seems work. when trigger @ second time, image view not animate , stay atop of subviews.
codes of view controller(with arc):
firstly, ibaction trigger snapshot:
- (ibaction)actionsavesnapshot:(id)sender { ... // save image file. works everytime. nsthread *tempthread = [[nsthread alloc] initwithtarget:self selector:@selector(threadsimulatescreencapture:) object:nil]; if (tempthread) { [tempthread start]; tempthread = nil; // release } }
and here thread:
- (void)threadsimulatescreencapture:(id)arg {@autoreleasepool { nsimageview __strong *subimageview; subimageview = [[nsimageview alloc] init]; nsrect subrect = [_imageview frame]; // dynamic image view want snapshot const cgfloat animationtime = 0.4; [subimageview setwantslayer:yes]; [subimageview setimagescaling:nsimagescaleaxesindependently]; [subimageview setimage:[_imageview image]]; dispatch_async(dispatch_get_main_queue(), ^{ [catransaction begin]; [self.view addsubview:subimageview]; [subimageview setframe:subrect]; [subimageview setneedsdisplay:yes]; [catransaction commit]; }); [nsthread sleepfortimeinterval:0.01]; dispatch_async(dispatch_get_main_queue(), ^{ [catransaction begin]; [catransaction setanimationduration:animationtime]; [subimageview.layer setframe:cgrectzero]; [catransaction commit]; }); [nsthread sleepfortimeinterval:(nstimeinterval)animationtime]; // wait end of animation dispatch_async(dispatch_get_main_queue(), ^{ [subimageview removefromsuperview]; }); nslog(@"subview count: %@\n%@", self.view.subviews, subimageview); // mark. described below subimageview = nil; // release image }} // ends: thread , aotureleasepool
every time program runs "mark" line , print subview array, clear thar subimageview
not removed superview then. , every time thread ends, "deleted thread uncommitted catransaction ...." raised.
what wrong did do?
i may solved own problem: @ line [subimageview setframe:subrect];
, insert line with: [subimageview.layer setframe:subrect];
meanwhile, add line before "addsubview:" method: [self.view setwantslayer:yes];
.
it seemed work. not know why. why should set both view , layer's frame?
Comments
Post a Comment