KVO机制抛出错误?

时间:2013-02-26 09:14:22

标签: iphone ios objective-c ipad key-value-observing

我实现了一个照片编辑应用程序,其中我有一个摄像头捕获的图像,一个是输出图像,我在捕获的图像旁边显示。每当我更改捕获的图像缩放或旋转时,我希望将此更改通知outputImage。对于这个senario,我写了以下代码,

-(void)imageCapturedByCameraAPI:(UIImage *)theCapturedImage{
[[arOverlayVC view] removeFromSuperview];
capturedImageView.image = theCapturedImage;
[self.view addSubview:imageEditorView];

rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotatePiece:)];
[capturedImageView addGestureRecognizer:rotationGesture];
**//KVO added for rotation**
[capturedImageView addObserver:self forKeyPath:@"transform.rotate" options:NSKeyValueObservingOptionNew context:NULL];

pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scalePiece:)];
[pinchGesture setDelegate:self];
[capturedImageView addGestureRecognizer:pinchGesture];
**//KVO added for scaling**
[capturedImageView addObserver:self forKeyPath:@"transform.scale" options:NSKeyValueObservingOptionNew context:NULL];

}

   - (void)rotatePiece:(UIRotationGestureRecognizer *)gestureRecognizer {
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
    [gestureRecognizer view].transform = CGAffineTransformRotate([[gestureRecognizer view] transform], [gestureRecognizer rotation]);
    [self editedImage];
    [gestureRecognizer setRotation:0];
}
 }

- (void)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer {
if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
    lastScale = [gestureRecognizer scale];
}

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
    [gestureRecognizer state] == UIGestureRecognizerStateChanged) {

    CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];

    const CGFloat kMaxScale = 2.0;
    const CGFloat kMinScale = 1.0;

    CGFloat newScale = 1 -  (lastScale - [gestureRecognizer scale]);
    newScale = MIN(newScale, kMaxScale / currentScale);
    newScale = MAX(newScale, kMinScale / currentScale);
    CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
    [gestureRecognizer view].transform = transform;
    lastScale = [gestureRecognizer scale];  
}
 }

  - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
// if the gesture recognizers are on different views, don't allow simultaneous recognition

if (gestureRecognizer.view != otherGestureRecognizer.view)
    return NO;

// if either of the gesture recognizers is the long press, don't allow simultaneous recognition
if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]] || [otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
    return NO;

return YES;
   }

 - (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer    {
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
    UIView *piece = gestureRecognizer.view;
    CGPoint locationInView = [gestureRecognizer locationInView:piece];
    CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];

    piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
    piece.center = locationInSuperview;
}
  }

现在我的观察方法是,

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
   {
if(object == capturedImageView && ([keyPath isEqualToString:@"transform.rotate"] || [keyPath isEqualToString:@"transform.scale"])) {
    NSLog(@"KVO detected");
     }
  }

我发现以下错误,

  

类NSConcreteValue的实例0x189f40在释放时被释放   关键价值观察员仍在注册。观察信息   被泄露了,甚至可能被错误地附在其他人身上   宾语。在NSKVODeallocateBreak上设置断点以在此处停止   调试器。这是目前的观察信息:    (上下文:0x0,属性:0x189ff0>

     

类NSConcreteValue的实例0x188cb0在释放时被释放   关键价值观察员仍在注册。观察信息   被泄露了,甚至可能被错误地附在其他人身上   宾语。在NSKVODeallocateBreak上设置断点以在此处停止   调试器。这是目前的观察信息:    (上下文:0x0,属性:0x188d80>

     

* 由于未捕获的异常而终止应用   'NSInternalInconsistencyException',原因:'无法更新   观察者的关键路径   “transform.scale”来自,很可能是因为   没有适当的KVO,键“变换”的值已经改变   发送通知。检查UIImageView的KVO兼容性   类“。

2 个答案:

答案 0 :(得分:1)

正如错误消息所说

  

一个类的实例被释放,而键值观察者仍然在其中注册

您需要在使用capturedImageView

取消分配removeObserver:forKeyPath:context:之前删除观察结果

确保平衡注册/注销计数以便不破坏KVO非常重要。

我想了解更多有关KVO的信息,此链接很有用:http://www.mikeash.com/pyblog/key-value-observing-done-right.html

和第2部分:http://www.mikeash.com/pyblog/friday-qa-2012-03-02-key-value-observing-done-right-take-2.html

答案 1 :(得分:1)

arc仅处理NSObject& Co的内存,用于手动清理所需的其他所有内容(例如KVO或网络连接或文件写入)

覆盖 - (void)dealloc for

- (void)dealloc {
    [capturedImageView removeObserver:self forKeyPath:@"transform.scale"];   
}