0

我正在实现一个照片编辑应用程序,其中我有一个相机捕获的图像,一个是输出图像,它显示在捕获的图像旁边。每当我更改捕获的图像缩放或旋转时,我都想将此更改通知给 outputImage。对于这个场景,我编写了以下代码,

-(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”,很可能是因为关键“transform”的值已更改而未发送适当的 KVO 通知。检查 UIImageView 类的 KVO 合规性。

4

2 回答 2

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

于 2013-02-26T09:26:01.120 回答
1

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

为此覆盖 -(void)dealloc

- (void)dealloc {
    [capturedImageView removeObserver:self forKeyPath:@"transform.scale"];   
} 
于 2013-02-26T09:32:36.510 回答