SimpleCam icon indicating copy to clipboard operation
SimpleCam copied to clipboard

camera setup is really slow on very first start on device

Open pawelnathan opened this issue 9 years ago • 5 comments

On the very first start on a device it takes maybe 20 sec or more to start up simplecam. On the second start it opens immediately. I tried with the demo supplied. If I delete the app and restart it again, it is fast.

Does anybody have an idea?

pawelnathan avatar Jan 11 '16 12:01 pawelnathan

Hi @plenio I'm not sure when I'll get a chance to profile this, have you by chance run instruments to see if you can isolate what's causing the lag?

loganwright avatar Jan 11 '16 23:01 loganwright

Hi @LoganWright I have now found a way to reproduce it easily. Just open the SimpleCam sample and change the bundle identifier. If you open it on iphone4s it takes about 15 secs to open camera.

The app seems to wait for a very long time doing nothing, even in the profiler. There is only a single function that takes very long cpu time(888 ms) which is the call tree capturePhoto(line 568)-crop-drawInRect(line 822).

This is all I can say. Does that give you a clue?

Regards, Werner

pawelnathan avatar Jan 26 '16 12:01 pawelnathan

I could reproduce in debug environment and get an exception now.

It crashes in line 216 - [self.view addSubview:_imageStreamV];

Here is the log: This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release. Stack:( 0 CoreFoundation 0x20dcc123 + 150 1 libobjc.A.dylib 0x20572e17 objc_exception_throw + 38 2 CoreFoundation 0x20dcc051 + 0 3 Foundation 0x216acacb + 170 4 Foundation 0x215523bf + 38 5 UIKit 0x24f0394f + 454 6 UIKit 0x24f108dd + 1660 7 UIKit 0x24f10257 + 30 8 !Plenio 0x001ad29b -[SimpleCam setup] + 982 9 !Plenio 0x001aca35 __27-[SimpleCam viewDidAppear:]_block_invoke + 60 10 TCC 0x2241db95 + 244 11 TCC 0x2241fc05 + 28 12 libxpc.dylib 0x20b2247b + 30 13 libxpc.dylib 0x20b2242b + 26 14 libdispatch.dylib 0x00733fe7 _dispatch_root_queue_drain + 1790 15 libdispatch.dylib 0x007338e7 _dispatch_worker_thread3 + 102 16 libsystem_pthread.dylib 0x20b03b29 _pthread_wqthread + 1024 17 libsystem_pthread.dylib 0x20b03718 start_wqthread + 8

pawelnathan avatar Jan 26 '16 13:01 pawelnathan

OK, I have a fix for this.

In viewDidAppear change the code following this code:

        case AVAuthorizationStatusNotDetermined: {
            // not determined
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                if(granted){

with this

                if(granted){
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self setup];
                        [self animateIntoView];
                    });

This should do for ios8+. Maybe there is something else necessary for ios < 8 Hope this is of help to someone

pawelnathan avatar Jan 26 '16 14:01 pawelnathan

@plenio Would you be willing to submit a PR for this fix? I'm fine w/ also bumping minimum support to 8.0.

Thanks again for bringing this up!

loganwright avatar Jan 31 '16 01:01 loganwright