我的应用情况:
我现在有一个显示动态图片的应用。我想捕获它的当前图像并将其保存到光盘。
我的目标:
我正在制作一个动画视觉效果:在顶部添加一个图像视图,然后将其从其原始帧动画到 CGRecrZero。最后,在动画完成后从 vie 层次结构中删除这个图像视图。
现在的问题:
第一次触发动作,它似乎工作。但是当第二次触发它时,图像视图不会动画并停留在所有子视图的顶部。
视图控制器的代码(使用 ARC):
首先,这是触发快照的 IBAction:
- (IBAction)actionSaveSnapshot:(id)sender
{
... // Save the image file. This works everytime.
NSThread *tempThread = [[NSThread alloc] initWithTarget:self
selector:@selector(threadSimulateScreenCapture:)
object:nil];
if (tempThread)
{
[tempThread start];
tempThread = nil; // Simply release it
}
}
这是线程:
- (void)threadSimulateScreenCapture:(id)arg
{@autoreleasepool {
NSImageView __strong *subImageView;
subImageView = [[NSImageView alloc] init];
NSRect subRect = [_imageView frame]; // the dynamic image view which I want to snapshot with
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 for 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 the image
}} // ENDS: thread and aotureleasepool
每次程序运行到“MARK”行并打印子视图数组时,很明显subImageView
不会从超级视图中删除。并且每次线程结束时,都会引发一个“带有未提交 CATransaction .... 的已删除线程”。
我做错了什么?