Merge pull request #7178 from valeriyvan:iosfixes

* Changes delegate property from assign to weak

In modern Objective-C delegate should be weak. In very rare conditions you might want delegate be strong.
Assign for delegate is sign of legacy code.
This change prevents crash when you forget nil delegate in dealloc and makes rush with nilling delegate unnecessary.
This change shouldn't break any existing code.

* Adds implementation for setters and getters for weak delegate properties for non ARC Obj-C files

For whatever reason compiler can't synthesize these.
And yes, it's time to convert all Objective-C stuff to ARC.
pull/7287/head
Valeriy Van 9 years ago committed by Alexander Alekhin
parent 473dba1189
commit f1dcf71dd7
  1. 4
      modules/videoio/include/opencv2/videoio/cap_ios.h
  2. 13
      modules/videoio/src/cap_ios_photo_camera.mm
  3. 18
      modules/videoio/src/cap_ios_video_camera.mm

@ -133,7 +133,7 @@
} }
@property (nonatomic, assign) id<CvVideoCameraDelegate> delegate; @property (nonatomic, weak) id<CvVideoCameraDelegate> delegate;
@property (nonatomic, assign) BOOL grayscaleMode; @property (nonatomic, assign) BOOL grayscaleMode;
@property (nonatomic, assign) BOOL recordVideo; @property (nonatomic, assign) BOOL recordVideo;
@ -167,7 +167,7 @@
AVCaptureStillImageOutput *stillImageOutput; AVCaptureStillImageOutput *stillImageOutput;
} }
@property (nonatomic, assign) id<CvPhotoCameraDelegate> delegate; @property (nonatomic, weak) id<CvPhotoCameraDelegate> delegate;
- (void)takePicture; - (void)takePicture;

@ -36,6 +36,9 @@
@interface CvPhotoCamera () @interface CvPhotoCamera ()
{
id<CvPhotoCameraDelegate> _delegate;
}
@property (nonatomic, strong) AVCaptureStillImageOutput* stillImageOutput; @property (nonatomic, strong) AVCaptureStillImageOutput* stillImageOutput;
@ -53,8 +56,14 @@
#pragma mark Public #pragma mark Public
@synthesize stillImageOutput; @synthesize stillImageOutput;
@synthesize delegate;
- (void)setDelegate:(id<CvPhotoCameraDelegate>)newDelegate {
_delegate = newDelegate;
}
- (id<CvPhotoCameraDelegate>)delegate {
return _delegate;
}
#pragma mark - Public interface #pragma mark - Public interface
@ -106,9 +115,7 @@
cameraAvailable = YES; cameraAvailable = YES;
NSLog(@"CvPhotoCamera captured image"); NSLog(@"CvPhotoCamera captured image");
if (self.delegate) {
[self.delegate photoCamera:self capturedImage:newImage]; [self.delegate photoCamera:self capturedImage:newImage];
}
[self.captureSession startRunning]; [self.captureSession startRunning];
}); });

@ -61,11 +61,12 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}
@implementation CvVideoCamera @implementation CvVideoCamera
{
id<CvVideoCameraDelegate> _delegate;
}
@synthesize delegate;
@synthesize grayscaleMode; @synthesize grayscaleMode;
@synthesize customPreviewLayer; @synthesize customPreviewLayer;
@ -78,7 +79,13 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}
@synthesize recordPixelBufferAdaptor; @synthesize recordPixelBufferAdaptor;
@synthesize recordAssetWriter; @synthesize recordAssetWriter;
- (void)setDelegate:(id<CvVideoCameraDelegate>)newDelegate {
_delegate = newDelegate;
}
- (id<CvVideoCameraDelegate>)delegate {
return _delegate;
}
#pragma mark - Constructors #pragma mark - Constructors
@ -450,7 +457,8 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}
{ {
(void)captureOutput; (void)captureOutput;
(void)connection; (void)connection;
if (self.delegate) { auto strongDelegate = self.delegate;
if (strongDelegate) {
// convert from Core Media to Core Video // convert from Core Media to Core Video
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
@ -492,8 +500,8 @@ static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}
CGImage* dstImage; CGImage* dstImage;
if ([self.delegate respondsToSelector:@selector(processImage:)]) { if ([strongDelegate respondsToSelector:@selector(processImage:)]) {
[self.delegate processImage:image]; [strongDelegate processImage:image];
} }
// check if matrix data pointer or dimensions were changed by the delegate // check if matrix data pointer or dimensions were changed by the delegate

Loading…
Cancel
Save