Frequently Camera capture crashing the app without any clue - ios

I am using UIIMagePicker Controller for capturing image. The camera capture works fine for first 30 to 40 shots but it will crash the app after around 40 captures. I do not get any memory warning or crash report on xcode.
This issue look like memory leak but i have monitored Instruments and memory use is not going more then 60 MB.
The image pick from gallery do not cause this issue.
Code i am using :-
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[MBProgressHUD showHUDAddedTo:self.view animated:true];
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"MMM-dd-yyyy";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
timeFormatter.dateFormat = #"HH:mm a";
[timeFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *strtime=[NSString stringWithFormat:#"%#\n%# ",[timeFormatter stringFromDate:now],[dateFormatter stringFromDate:now]];
lblTime.numberOfLines =0;
[lblTime setText:strtime];
[lblTime setHidden:YES];
imgTakenPhoto.image = chosenImage;
[btnCrossOnDentView setHidden:YES];
[btnDoneWithDent setHidden:YES];
App_Delegate.isEdited = YES;
[picker dismissViewControllerAnimated:YES completion: ^{
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideAllHUDsForView:self.view animated:true];
imagePickerController = nil;
}); }];
}

Its an iPhone OS issue. I also submitted a report regarding this issue. Same code will run fine in iPad. Report a bug and submit your project.

The best workaround to this is that instead of using UIImagePicker for Camera, use your custom camera view as like WhatsApp, Provide one Capture button onto it to capture as many pics as you want.
Use AVCapture
This will completely remove the burden of opening the Camera Controller.

I also faced this problem my current working application. This is because of memory overflow while running your application. I removed unnecessary memory space in application running state. Now my application working fine without any problem.

Related

if my iOS app freezes the entire phone, is this considered a bug in the OS itself?

I am working on an iOS app where I am using AVPlayer to play different mp4 videos. Most of the times it works fine. except sometimes my app completely freezes the phone. I haven't been able to catch it at what place this happens but I think it usually happens right after this line. I verified this by placing a bunch of NSLog where I print the [[NSDate date] timeIntervalSince1970] :
mylayer =[AVPlayerLayer playerLayerWithPlayer:myplayer];
The freeze happens for a few seconds (sometimes much longer).
Even if I press home button or lock button, the phone is unresponsive.
I have to end up pressing the lock button for about 6-10 seconds which hard restarts the entire phone.
Note that the CPU and memory usage doesn't spike during this.
I understand my code might be buggy and all but shouldn't the OS be intelligent enough to not let a single app completely freeze the entire phone? Would this be considered an OS bug? If so, I might log a DTS with Apple.
****EDIT: added code****
Note the comment which says "// this is the line which freezes"
dispatch_queue_t LOADQUEUE = dispatch_queue_create("com.yolo.LOADQUEUE", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(LOADQUEUE, ^{
AVURLAsset *avAsset = [[AVURLAsset alloc] initWithURL:url options:nil];
NSLog(#"current time 4: %f",[[NSDate date] timeIntervalSince1970]);
if ([avAsset tracksWithMediaType:AVMediaTypeVideo] && [avAsset tracksWithMediaType:AVMediaTypeVideo].count>0) {
NSLog(#"current time 4.5: %f",[[NSDate date] timeIntervalSince1970]);
CGSize size = [[[avAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] naturalSize];
NSLog(#"current time 5: %f",[[NSDate date] timeIntervalSince1970]);
CGRect r = self.topHeader.frame;
r.size.height=((size.height*self.view.frame.size.width)/size.width)+self.topheaderBottomView.frame.size.height+self.topheadertopview.frame.size.height+self.itemTitle.frame.size.height;
howMuchToScrollToShowCommentButton=r.size.height;
dispatch_async(dispatch_get_main_queue(), ^{
self.topHeader.frame=r;
[UIView animateWithDuration:0 animations:^{
[self.mytableview setTableHeaderView:self.topHeader];
}completion:^(BOOL finished) {
NSArray *keys = #[#"playable"];
NSLog(#"current time 6: %f",[[NSDate date] timeIntervalSince1970]);
[avAsset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"current time 7: %f",[[NSDate date] timeIntervalSince1970]);
AVPlayerItem *newItem = [[AVPlayerItem alloc] initWithAsset:avAsset];
if (!myplayer) {
myplayer = [[AVPlayer alloc]initWithPlayerItem:newItem];
} else {
[myplayer replaceCurrentItemWithPlayerItem:newItem];
}
NSLog(#"current time 7.5: %f",[[NSDate date] timeIntervalSince1970]);
[myplayer addObserver:self forKeyPath:#"status" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial) context:nil];
[myplayer addObserver:self forKeyPath:#"rate" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial) context:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[myplayer currentItem]];
myplayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
NSLog(#"current time 7.6: %f",[[NSDate date] timeIntervalSince1970]);
mylayer =[AVPlayerLayer playerLayerWithPlayer:myplayer]; // this is the line which freezes
NSLog(#"current time 7.7: %f",[[NSDate date] timeIntervalSince1970]);
[playerView.layer addSublayer:mylayer];
mylayer.videoGravity = AVLayerVideoGravityResize;
[mylayer setFrame:playerView.bounds];
[myplayer seekToTime:kCMTimeZero];
NSLog(#"current time 8: %f",[[NSDate date] timeIntervalSince1970]);
});
}];
}];
});
} else {
NSLog(#"ITEM doesn't exist");
}
});
Output:
Note the 21 second break between time 7.6 and 7.7 :
2016-06-04 01:27:20.853 XYZ[402:49072] current time 7: 1465018040.853897
2016-06-04 01:27:20.875 XYZ[402:49072] current time 7.5: 1465018040.875220
2016-06-04 01:27:20.875 XYZ[402:49072] current time 7.6: 1465018040.875871
2016-06-04 01:27:41.841 XYZ[402:49072] current time 7.7: 1465018061.841419
2016-06-04 01:27:41.841 XYZ[402:49072] current time 8: 1465018061.841863
Edit 2:
I paused the app in xcode and looked at what the threads were doing on the left. Here's a screenshot:
This could be a symptom of debugging and running through XCode. You're right, normally you should always be able to hit the Home button and exit the application.
Edit your scheme and change from Debug to Release. Run a build once through Xcode. Kill the app, then Launch it without Xcode from the Home screen of the device.

Crash with MWPhotoBrowser

I've imported last version of MWPhotoBrowser classes in my project, but when the browser load images form web the app crashes. In implementation i use the same code founded in demo project, if i show local images it's all ok. This is my implementation:
[galleryPhotosArray addObject:[MWPhoto photoWithImage:[UIImage imageNamed:#"Blue_Screen_iPhone.png"]]];
[galleryPhotosArray addObject:[MWPhoto photoWithImage:[UIImage imageNamed:#"Logo_iPhone.png"]]];
[galleryPhotosArray addObject:[MWPhoto photoWithURL:[NSURL URLWithString:#"http://www.cellulare.it/wp-content/uploads/2013/09/apple1.png"]]];
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
browser.displayActionButton = YES;
browser.displayNavArrows = NO;
browser.wantsFullScreenLayout = YES;
browser.zoomPhotosToFill = YES;
[browser setCurrentPhotoIndex:0];
[self.navigationController pushViewController:browser animated:YES];
[browser release];
so i add two local images and a random image from google images. The app crashes in two points, sometimes in SDWebImageDownloaderOperation.m in this line:
self.connection = [NSURLConnection.alloc initWithRequest:self.request delegate:self startImmediately:NO];
the second point is in SDImageCache.m file, in - (NSString *)cachePathForKey:(NSString *)key inPath:(NSString *)path method
Somebody has this issues?
My error, i've only added ARC support to SDWebImage files.

Memory leak while using performSelectorInBackground

I am new to iOS development. I am facing problems while running a background thread. In my code resetUi is running on main UI thread, Now I am starting a background thread to fetch an image data and update my image. Everything works fine, but memory is leaking when call is made to performSelectorInBackground.
Please let me know where I am doing wrong. Also please suggest if there is a better way to update my image while fetching from an URL(dataWithContentsOfURL).
[Update]
Instrument is displaying 2 separate leaks one at perfromSelectorInBackground and other at UIImage imageWithData. I guess something is going terribly wrong with imageupdate(imageWithData)
-(void)updateData{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
profileName.text = oAuthTwitter.screen_name;
if(profilePic.image == nil){
NSString *urlString = #"https://api.twitter.com/1/users/profile_image/";
urlString = [urlString stringByAppendingFormat:oAuthTwitter.screen_name];
urlString = [urlString stringByAppendingFormat:#"?size=bigger"];
profilePic.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]];
[activityIndicator stopAnimating];
[activityIndicator release];
}
[pool drain];
}
- (void)resetUi{
if (oAuthTwitter.oauth_token_authorized) {
profilePic.hidden = NO;
profileName.hidden = NO;
NSLog(#"Resetting to authorised state");
[self performSelectorInBackground:#selector(updateData) withObject:nil];
}else{
NSLog(#"Resetting Twitter UI to non-authorized state.");
profilePic.hidden = YES;
profileName.hidden = YES;
}
}
I think you should use
[pool release];
rather than
[pool drain];
which is far more better practice.
Also can you try to release activityIndicator in the main thread?
From the code you have given I can't find any other cause for leak.. Have you tried to run your code using leak instrument and static analyzer?

iPhone Abbyy OCR

I'm working on an iphone app that uses Abbyy OCR.
Using the wrapper class for iphone there is a method :
[ocrManager recognizeImage:[choosenImage image] withCallback:self];
a UIImage is passed as a parameter which is used to recognize characters. But every time i receive exception "Required Data File Missed".
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
if(check == 1)
{
return;
}
check = 1;
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[choosenImage setImage:image];
[process setHidden:NO];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"license" ofType:#""];
NSData *license = [[NSData alloc] initWithContentsOfFile:filePath];
CMocrManager *ocrManager = [CMocrManager createManager:license];
NSSet *languages = [[NSSet alloc] initWithArray:[NSArray arrayWithObject:#"English"]];
[ocrManager setLanguages:languages];
[ocrManager setDefaultImageResolution:0];
#try {
[ocrManager recognizeImage:[choosenImage image] withCallback:self];
}
#catch (NSException *exception) {
NSString *ex = exception.reason;
}
CMocrLayout* recognitionResult = [ocrManager copyRecognitionResults];
NSArray* strings = [recognitionResult copyStrings];
}
the image can be seen on UIImageView but when I pass it to recognizeImage method it throws the exception.
I've been researching a lot but unable to find a solution. Any help is appreciated.
My name is Nikolay Khlebinsky, i work # ABBYY.
"Required Data File Missed" error message is displayed when resource files are missing (keywords, patterns or dictionaries). Refer to «How to Work with the ABBYY Mobile OCR Engine Library on the iPhone» help article for project organization guide. You can also look for iPhone project sample in the engine distributive.
If you would still experience any difficulties, please visit our technical support contacts page at http://www.abbyy.com/support/contacts/ Choose your country and your product, hit 'Search' and you'll get contacts of the proper ABBYY representatives. Contacting them is the fastest way to solve technical issues.

UIImagePickerController run out of memory with camera source

I got a big performance issue using UIImagePickerController and saving the image on disk. I can't figure out what I am doing wrong. Here is my code:
- (void)imagePickerController:(UIImagePickerController *)pick
didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
iPixAppDelegate *delegate = (iPixAppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate addPicture:imageData];
}
The addPicture method creates a new picture object that is initialized this way:
- (Picture*) initPicture:(NSData*)dat inFolder:(NSString*)pat {
self.data = dat;
NSDate *d = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-mm-dd hh-mm-ss"];
self.name = [[formatter stringFromDate:d] stringByAppendingString:#".png"]; //The name by default of a picture is the date it has been taken
[formatter release];
self.path = [pat stringByAppendingPathComponent:self.name];
if(![self fileExistsAtPath:self.path]){
[self.data writeToFile:self.path atomically:YES];
}
return self;
}
The UIImagePickerController is quite fast but the program becomes very slow when I save picture on the disk.
Any idea on what I am doing wrong?
I had a similar issue. The way I got round it was to handle the image from the picker in a seperate thread. My problem was the main thread handling my app/UI was crashing out when trying to close the picker and handle the image:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
NSLog(#"picker did finish");
[NSThread detachNewThreadSelector:#selector(useImage:) toTarget:self withObject:image];
}
Your problem might be due to you taking the original image.
The original image from the camera has a resolution of around 1200x1400, which is a lot of memory and will cause the device to crash if you try making a picture out of it (it will run out of memory).
I would suggest resizing the image to be smaller (the native 320x480).

Resources