Comments (9)
Hey @chazsmi - can you try calling startScanningWithResultBlock
in viewDidAppear
instead of viewWillAppear
? I've seen this work in the past with Apple's AVFoundation sample project.
If that's unacceptable for your project for some reason, I would try using the raw AVFoundation classes and narrow down if the issue exists there or in MTBBarcodeScanner. I'm happy to help with this if you can wait until the weekend!
from mtbbarcodescanner.
Hi @mikebuss, thanks for your quick reply. Tried viewDidAppear and the preview didnt load at all. Any help you can give would be great. Thanks again
from mtbbarcodescanner.
I modified the sample project to start/stop scanning in viewDidAppear and viewWillDissappear respectively and linked it below. Can you let me know what your project is doing differently than this one? This seems to work on my end.
https://dl.dropboxusercontent.com/u/1462749/MTBBarcodeScanner-9-1-2015.zip
from mtbbarcodescanner.
I'm going to close this for now as it seems to be working in the demo project I linked. If you encounter this issue, please try to replicate it in the project above and I'll be happy to take a look!
from mtbbarcodescanner.
Hi Mike,
Sorry I'v been slow to reply. Only got back to this today, a little bit stumped ive looked at your code and tried to make my methods implement the scanner in the same way. Here is my code:
- (void)viewDidAppear:(BOOL)animated
{
_util = [[Utils alloc] init];
if ([self.scanner isScanning]) {
[self.scanner stopScanning];
} else {
[MTBBarcodeScanner requestCameraPermissionWithSuccess:^(BOOL success) {
if (success) {
[self startScanning];
} else {
[_util showAlert:@"No camera permission" msg:@"You need to give permission to you use your camera to use the scanner"];
}
}];
}
[super viewDidAppear:animated];
}
and here is my startScanning method
- (void)startScanning {
self.uniqueCodes = [[NSMutableArray alloc] init];
[self.scanner startScanningWithResultBlock:^(NSArray *codes) {
for (AVMetadataMachineReadableCodeObject *code in codes) {
if (code.stringValue && [self.uniqueCodes indexOfObject:code.stringValue] == NSNotFound) {
[self.uniqueCodes addObject:code.stringValue];
NSLog(@"Found unique code: %@", code.stringValue);
NSString *way = @"plus";
if (self.refControl.selectedSegmentIndex == 0) {
// item finished
way = @"minus";
}
if ([self.quantity.text isEqualToString:@""]) {
self.quantity.text = @"1";
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *userId = [defaults objectForKey:@"userID"];
NSLog(@"%@", self.quantity.text);
NSDictionary *params = @{@"barcode" : code.stringValue,
@"way" : way,
@"quantity" : self.quantity.text,
@"userId" : userId};
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString* HOST_URL = [NSString stringWithFormat:@"%@%@", appDelegate.URL, @"/api/v1/getitembybarcode"];
[_util showLoading:self.camera];
AFHTTPRequestOperationManager *operationManager = [_util setCache];
[operationManager POST:HOST_URL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject){
if ([responseObject[@"success"] intValue] == 1)
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"RELOAD_DATA"object:nil];
NSString *alertMsg = [NSString stringWithFormat:@"%@%@", responseObject[@"responseData"][@"name"], @" updated"];
[_util showAlert:alertMsg msg:alertMsg];
} else {
[_util showAlert:@"Error finding product" msg:responseObject[@"responseData"]];
}
[_util stopLoading];
}failure:^(AFHTTPRequestOperation *operation, NSError *error){
// Enter what happens here if failure happens
NSLog(@"%@su", error);
}];
}
}
}];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.scanner stopScanning];
[super viewWillDisappear:animated];
}
Thanks for your help
from mtbbarcodescanner.
That code looks fine to me. Can you try doing an NSAssert(self.scanner)
at the top of startScanning
to make sure the scanner isn't nil when you come back to the view? I don't see the code where self.scanner
is created, so that's my first guess.
MTBBarcodeScanner will assert that a camera is present and scanning is permitted when you call startScanningWithResultBlock:
, so there aren't really any failure states. You might check if the previewView
is nil when you come back to the view as well.
Another idea would be to add a button to start/stop scanning (just as a test) and make sure you can toggle scanning that way. If so, we can narrow it down to the UIView willAppear willDisappear methods.
Hope this helps.
from mtbbarcodescanner.
Hi Mike,
Here is the console after NSLogging the self.scanner and the UIView after loading the view for the first time then going away and back again:
2015-09-15 21:25:49.722 [647:196033] Scanner called
2015-09-15 21:25:49.723 [647:196033] (null)
2015-09-15 21:25:49.725 [647:196033] <UIView: 0x14561c1b0; frame = (0 56; 600 544); autoresize = RM+BM; layer = <CALayer: 0x17422f520>>
2015-09-15 21:25:53.100 [647:196033] Scanner called
2015-09-15 21:25:53.100 [647:196033] <MTBBarcodeScanner: 0x170270640>
2015-09-15 21:25:53.100 [647:196033] <UIView: 0x14561c1b0; frame = (0 56; 600 0); autoresize = RM+BM; layer = <CALayer: 0x17422f520>>
and heres the whole file:
@interface ScanViewController ()
@property (nonatomic, strong) MTBBarcodeScanner *scanner;
@property (nonatomic, strong) NSMutableArray *uniqueCodes;
@property (nonatomic, weak) IBOutlet UIView *camera;
@property (nonatomic, strong) Utils *util;
@end
@implementation ScanViewController
- (void)viewWillAppear:(BOOL)animated
{
_util = [[Utils alloc] init];
NSLog(@"Scanner called");
NSLog(@"%@", self.camera);
// if (self.scanner) {
self.scanner = [[MTBBarcodeScanner alloc] initWithPreviewView:self.camera];
//}
if ([self.scanner isScanning]) {
[self.scanner stopScanning];
} else {
[MTBBarcodeScanner requestCameraPermissionWithSuccess:^(BOOL success) {
if (success) {
[self startScanning];
} else {
[_util showAlert:@"No camera permission" msg:@"You need to give permission to you use your camera to use the scanner"];
}
}];
}
[super viewWillAppear:animated];
}
//- (MTBBarcodeScanner *)scanner {
// NSLog(@"Scanner called");
// NSLog(@"%@", self.scanner);
// NSLog(@"%@", self.camera);
// if (self.scanner) {
// self.scanner = [[MTBBarcodeScanner alloc] initWithPreviewView:self.camera];
// }
// return self.scanner;
//}
- (void)startScanning {
self.uniqueCodes = [[NSMutableArray alloc] init];
[self.scanner startScanningWithResultBlock:^(NSArray *codes) {
for (AVMetadataMachineReadableCodeObject *code in codes) {
if (code.stringValue && [self.uniqueCodes indexOfObject:code.stringValue] == NSNotFound) {
[self.uniqueCodes addObject:code.stringValue];
NSLog(@"Found unique code: %@", code.stringValue);
NSString *way = @"plus";
if (self.refControl.selectedSegmentIndex == 0) {
// item finished
way = @"minus";
}
if ([self.quantity.text isEqualToString:@""]) {
self.quantity.text = @"1";
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *userId = [defaults objectForKey:@"userID"];
NSLog(@"%@", self.quantity.text);
NSDictionary *params = @{@"barcode" : code.stringValue,
@"way" : way,
@"quantity" : self.quantity.text,
@"userId" : userId};
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString* HOST_URL = [NSString stringWithFormat:@"%@%@", appDelegate.URL, @"/api/v1/getitembybarcode"];
[_util showLoading:self.camera];
AFHTTPRequestOperationManager *operationManager = [_util setCache];
[operationManager POST:HOST_URL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject){
if ([responseObject[@"success"] intValue] == 1)
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"RELOAD_DATA"object:nil];
NSString *alertMsg = [NSString stringWithFormat:@"%@%@", responseObject[@"responseData"][@"name"], @" updated"];
[_util showAlert:alertMsg msg:alertMsg];
} else {
[_util showAlert:@"Error finding product" msg:responseObject[@"responseData"]];
}
[_util stopLoading];
}failure:^(AFHTTPRequestOperation *operation, NSError *error){
// Enter what happens here if failure happens
NSLog(@"%@su", error);
}];
}
}
}];
}
- (IBAction)actionChange:(id)sender
{
}
- (void)viewDidDisappear:(BOOL)animated {
[self.scanner stopScanning];
[super viewDidDisappear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
//- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
// [[self view] endEditing:YES];
//}
@end
from mtbbarcodescanner.
@chazsmi can you create a small project to illustrate the issue you're having? If you can send that my way, I can take a closer look.
The code you mentioned doesn't seem to produce the output you linked. It's outputting:
2015-09-15 21:25:53.100 [647:196033] <MTBBarcodeScanner: 0x170270640>
But the only line that outputs the scanner, like so:
NSLog(@"%@", self.scanner);
Is currently commented out:
//- (MTBBarcodeScanner *)scanner {
// NSLog(@"Scanner called");
// NSLog(@"%@", self.scanner);
// NSLog(@"%@", self.camera);
// if (self.scanner) {
// self.scanner = [[MTBBarcodeScanner alloc] initWithPreviewView:self.camera];
// }
// return self.scanner;
//}
So I'll need some sort of project to work with. If you want to start with the example I provided and try to break it with your use-case, I'd be happy to take a look if you could send it my way!
from mtbbarcodescanner.
Also it looks like the frame of the UIView changes from one log statement to the next. In the second one, the height is 0:
2015-09-15 21:25:53.100 [647:196033] <UIView: 0x14561c1b0; frame = (0 56; 600 0); autoresize = RM+BM; layer = <CALayer: 0x17422f520>>
from mtbbarcodescanner.
Related Issues (20)
- Only works with iOS10+ after recent changes HOT 2
- Impossible to read CODABAR barcode HOT 1
- Disable rotation HOT 1
- Initial camera setting HOT 1
- Warnings HOT 4
- Call stopScanning crash in viewDidDisappear HOT 4
- Flutter + Xcode 8.0: 3 errors, 9 warnings HOT 1
- Catalyst Support? HOT 4
- Maximum of read multiple codes HOT 3
- Problem configurating scanner with QR and Barcode HOT 5
- output becomes Garbled text HOT 8
- Crash NSInvalidArgumentException *** -[AVCaptureMetadataOutput setMetadataObjectTypes:] Unsupported type found - use -availableMetadataObjectTypes HOT 3
- when barcode data increased it is difficult to read Code.. HOT 1
- UIView preview has issues during orientation changes HOT 6
- Define Barcodetype in Swift HOT 3
- Camera Unfreeze method not working on. HOT 3
- Unrecognized Barcode Style HOT 1
- Camera unfreeze is corrupting flash HOT 3
- Getting the bytes raw HOT 1
- It's crashing on iPad and iPhone after you try to scan QR code. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mtbbarcodescanner.