Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Crash on iOS on low memory devices #304
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Dec 3, 2016
1 parent 87f50e9 commit 6e3718c
Showing 1 changed file with 40 additions and 38 deletions.
78 changes: 40 additions & 38 deletions src/ios/CDVBarcodeScanner.mm
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,20 @@ - (void)scan:(CDVInvokedUrlCommand*)command {

callback = command.callbackId;

NSDictionary* options = command.arguments.count == 0 ? [NSNull null] : [command.arguments objectAtIndex:0];

if ([options isKindOfClass:[NSNull class]]) {
NSDictionary* options;
if (command.arguments.count == 0) {
options = [NSDictionary dictionary];
} else {
options = command.arguments[0];
}

BOOL preferFrontCamera = [options[@"preferFrontCamera"] boolValue];
BOOL showFlipCameraButton = [options[@"showFlipCameraButton"] boolValue];
BOOL showTorchButton = [options[@"showTorchButton"] boolValue];
BOOL disableAnimations = [options[@"disableAnimations"] boolValue];

// We allow the user to define an alternate xib file for loading the overlay.
NSString *overlayXib = [options objectForKey:@"overlayXib"];
NSString *overlayXib = options[@"overlayXib"];

capabilityError = [self isScanNotPossible];
if (capabilityError) {
Expand All @@ -177,12 +179,12 @@ - (void)scan:(CDVInvokedUrlCommand*)command {
return;
}

processor = [[CDVbcsProcessor alloc]
initWithPlugin:self
callback:callback
parentViewController:self.viewController
alterateOverlayXib:overlayXib
];
processor = [[[CDVbcsProcessor alloc]
initWithPlugin:self
callback:callback
parentViewController:self.viewController
alterateOverlayXib:overlayXib
] autorelease];
// queue [processor scanBarcode] to run on the event loop

if (preferFrontCamera) {
Expand Down Expand Up @@ -228,8 +230,8 @@ - (void)encode:(CDVInvokedUrlCommand*)command {

- (void)returnImage:(NSString*)filePath format:(NSString*)format callback:(NSString*)callback{
NSMutableDictionary* resultDict = [[[NSMutableDictionary alloc] init] autorelease];
[resultDict setObject:format forKey:@"format"];
[resultDict setObject:filePath forKey:@"file"];
resultDict[@"format"] = format;
resultDict[@"file"] = filePath;

CDVPluginResult* result = [CDVPluginResult
resultWithStatus: CDVCommandStatus_OK
Expand All @@ -241,12 +243,12 @@ - (void)returnImage:(NSString*)filePath format:(NSString*)format callback:(NSStr

//--------------------------------------------------------------------------
- (void)returnSuccess:(NSString*)scannedText format:(NSString*)format cancelled:(BOOL)cancelled flipped:(BOOL)flipped callback:(NSString*)callback{
NSNumber* cancelledNumber = [NSNumber numberWithInt:(cancelled?1:0)];
NSNumber* cancelledNumber = @(cancelled ? 1 : 0);

NSMutableDictionary* resultDict = [[NSMutableDictionary alloc] init];
[resultDict setObject:scannedText forKey:@"text"];
[resultDict setObject:format forKey:@"format"];
[resultDict setObject:cancelledNumber forKey:@"cancelled"];
NSMutableDictionary* resultDict = [[NSMutableDictionary new] autorelease];
resultDict[@"text"] = scannedText;
resultDict[@"format"] = format;
resultDict[@"cancelled"] = cancelledNumber;

CDVPluginResult* result = [CDVPluginResult
resultWithStatus: CDVCommandStatus_OK
Expand Down Expand Up @@ -302,7 +304,7 @@ - (id)initWithPlugin:(CDVBarcodeScanner*)plugin
self.is1D = YES;
self.is2D = YES;
self.capturing = NO;
self.results = [NSMutableArray new];
self.results = [[NSMutableArray new] autorelease];

CFURLRef soundFileURLRef = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("CDVBarcodeScanner.bundle/beep"), CFSTR ("caf"), NULL);
AudioServicesCreateSystemSoundID(soundFileURLRef, &_soundFileObject);
Expand Down Expand Up @@ -383,7 +385,7 @@ - (BOOL)checkResult:(NSString *)result {
}

BOOL allEqual = YES;
NSString *compareString = [self.results objectAtIndex:0];
NSString *compareString = self.results[0];

for (NSString *aResult in self.results)
{
Expand Down Expand Up @@ -453,7 +455,7 @@ - (void)toggleTorch {
- (NSString*)setUpCaptureSession {
NSError* error = nil;

AVCaptureSession* captureSession = [[AVCaptureSession alloc] init];
AVCaptureSession* captureSession = [[[AVCaptureSession alloc] init] autorelease];
self.captureSession = captureSession;

AVCaptureDevice* __block device = nil;
Expand All @@ -475,7 +477,7 @@ - (NSString*)setUpCaptureSession {
AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) return @"unable to obtain video capture device input";

AVCaptureMetadataOutput* output = [[AVCaptureMetadataOutput alloc] init];
AVCaptureMetadataOutput* output = [[[AVCaptureMetadataOutput alloc] init] autorelease];
if (!output) return @"unable to obtain video capture output";

[output setMetadataObjectsDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)];
Expand Down Expand Up @@ -736,7 +738,7 @@ - (UIImage*)getImageFromSample:(CMSampleBufferRef)sampleBuffer {
//--------------------------------------------------------------------------
- (void)dumpImage:(UIImage*)image {
NSLog(@"writing image to library: %dx%d", (int)image.size.width, (int)image.size.height);
ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init];
ALAssetsLibrary* assetsLibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetsLibrary
writeImageToSavedPhotosAlbum:image.CGImage
orientation:ALAssetOrientationUp
Expand Down Expand Up @@ -880,7 +882,7 @@ - (void)loadView {
- (void)viewWillAppear:(BOOL)animated {

// set video orientation to what the camera sees
self.processor.previewLayer.connection.videoOrientation = (AVCaptureVideoOrientation)[[UIApplication sharedApplication] statusBarOrientation];
self.processor.previewLayer.connection.videoOrientation = (AVCaptureVideoOrientation) [[UIApplication sharedApplication] statusBarOrientation];

// this fixes the bug when the statusbar is landscape, and the preview layer
// starts up in portrait (not filling the whole view)
Expand Down Expand Up @@ -986,9 +988,9 @@ - (UIView*)buildOverlayView {
}
#else
if (_processor.isShowFlipCameraButton) {
items = [NSMutableArray arrayWithObjects:flexSpace, cancelButton, flexSpace, flipCamera, nil];
items = [@[flexSpace, cancelButton, flexSpace, flipCamera] mutableCopy];
} else {
items = [NSMutableArray arrayWithObjects:flexSpace, cancelButton, flexSpace, nil];
items = [@[flexSpace, cancelButton, flexSpace] mutableCopy];
}
#endif

Expand Down Expand Up @@ -1022,25 +1024,25 @@ - (UIView*)buildOverlayView {
[overlayView addSubview: toolbar];

UIImage* reticleImage = [self buildReticleImage];
UIView* reticleView = [[UIImageView alloc] initWithImage: reticleImage];
UIView* reticleView = [[[UIImageView alloc] initWithImage:reticleImage] autorelease];
CGFloat minAxis = MIN(rootViewHeight, rootViewWidth);

rectArea = CGRectMake(
0.5 * (rootViewWidth - minAxis),
0.5 * (rootViewHeight - minAxis),
minAxis,
minAxis
);
(CGFloat) (0.5 * (rootViewWidth - minAxis)),
(CGFloat) (0.5 * (rootViewHeight - minAxis)),
minAxis,
minAxis
);

[reticleView setFrame:rectArea];

reticleView.opaque = NO;
reticleView.contentMode = UIViewContentModeScaleAspectFit;
reticleView.autoresizingMask = 0
| UIViewAutoresizingFlexibleLeftMargin
| UIViewAutoresizingFlexibleRightMargin
| UIViewAutoresizingFlexibleTopMargin
| UIViewAutoresizingFlexibleBottomMargin
reticleView.autoresizingMask = (UIViewAutoresizing) (0
| UIViewAutoresizingFlexibleLeftMargin
| UIViewAutoresizingFlexibleRightMargin
| UIViewAutoresizingFlexibleTopMargin
| UIViewAutoresizingFlexibleBottomMargin)
;

[overlayView addSubview: reticleView];
Expand Down Expand Up @@ -1068,9 +1070,9 @@ - (UIImage*)buildReticleImage {
CGContextSetStrokeColorWithColor(context, color.CGColor);
CGContextSetLineWidth(context, RETICLE_WIDTH);
CGContextBeginPath(context);
CGFloat lineOffset = RETICLE_OFFSET+(0.5*RETICLE_WIDTH);
CGFloat lineOffset = (CGFloat) (RETICLE_OFFSET+(0.5*RETICLE_WIDTH));
CGContextMoveToPoint(context, lineOffset, RETICLE_SIZE/2);
CGContextAddLineToPoint(context, RETICLE_SIZE-lineOffset, 0.5*RETICLE_SIZE);
CGContextAddLineToPoint(context, RETICLE_SIZE-lineOffset, (CGFloat) (0.5*RETICLE_SIZE));
CGContextStrokePath(context);
}

Expand Down

1 comment on commit 6e3718c

@bekirbostanci
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not working now

Please sign in to comment.