CvPhotoCamera and memory warning - ios

I have a question about CvPhotoCamera (OpenCV) in the iOS App.
I have a myViewController1: in this viewController I push a mySecondView controller.
In this second View Controller I use CvPhotoCamera:
I have a UIImageViewController.
In viewDidLoad I have this code:
- (void)viewDidLoad
{
[super viewDidLoad];
_photoCamera = [[CvPhotoCamera alloc] initWithParentView:imageView];
_photoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPresetPhoto;
_photoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
[_photoCamera setDefaultAVCaptureDevicePosition:AVCaptureDevicePositionBack];
_photoCamera.delegate = self;
[_photoCamera start];
}
in viewDidDisappear I have this code:
- (void) viewDidDisappear:(BOOL)animated
{
[_photoCamera stop];
}
I use CvPhotoCamera to take a photo from camera using the method:
- (IBAction)actionStart:(id)sender;
{
[_photoCamera takePicture];
}
The my problem is this:
When I push this second ViewController and I tap on back button on Nav bar I have some memoryWarning and the app crashes ..always!!!
I used also the profile Xcode tool to manage memory allocation or memory leak but I do not see anything strange.
Is correct this use of CvPhotoCamera obj?

Related

iOS 9 custom transition - animationControllerForDismissedController not called

I am a newbee in iOS development and recently run into this problem with customized transition in iOS 9.
I have an object conforms to UIViewControllerTransitioningDelegate protocol and implements animationControllerForDismissedController, something like:
#implementation MyCustomizedTransitioningDelegate
#pragma mark - UIViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
MyCustomizedTransitionAnimator *animator = [[MyCustomizedTransitionAnimator alloc] init];
animator.presenting = NO;
return animator;
}
#end
And the process that triggers the modal transition is something like:
#implementation MyViewController
#pragma mark - Initializers
+ (MyCustomizedTransitioningDelegate *)modalTransitioningDelegateSingletonInstance;
{
static MyCustomizedTransitioningDelegate *delegateInst = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
delegateInst = [[MyCustomizedTransitioningDelegate alloc] init];
});
return delegateInst;
}
#pragma mark - UIViewController
- (void)dismissViewControllerAnimated:(BOOL)animated completion:(void (^)(void))completion;
{
[self prepareForDismissViewControllerAnimated:animated completion:&completion];
[super dismissViewControllerAnimated:animated completion:completion];
}
- (void)prepareForDismissViewControllerAnimated:(BOOL)animated completion:(dispatch_block_t *)completion;
{
self.presentedViewController.modalPresentationStyle = UIModalPresentationCustom;
self.presentedViewController.transitioningDelegate = [[self class] modalTransitioningDelegateSingletonInstance];
}
#end
Since animationControllerForDismissedController method is not called, the MyCustomizedTransitionAnimator is not created, which leads to its animateTransition not called either, which causes unexpected problem in my app. (Sorry for my poor English...)
I am also attaching the screenshot of stack trace for both iOS8 & iOS9.
In iOS 8, animationControllerForDismissedController is called after the stack trace below.
But in iOS9, transitionDidFinish is called somehow in advance, which I guess probably prevent animationControllerForDismissedController being called?
I was wondering if this is an iOS 9 bug or not. Any explanation or work around solution will be greatly appreciated!
I faced the same issue.
I hope this will help someone.
What fixed it for me is to make the object which applies UIViewControllerTransitioningDelegate protocol as variable instance to keep strong relationship with it.
I think because it gets dismissed after the view is presented first time.
I had the same issue.
Turned out I needed to set the delegate on the navigationController of the UIViewController that contains the trigger button.
Having this old code that didn't work:
UIViewController *dvc = [self sourceViewController];
TransitionDelegate *transitionDelegate = [TransitionDelegate new];
dvc.modalPresentationStyle = UIModalPresentationCustom;
dvc.transitioningDelegate = transitionDelegate;
[dvc dismissViewControllerAnimated:YES completion:nil];
I changed the first line to:
UIViewController *dvc = [self sourceViewController].navigationController;
and it worked.
Hope this helps.
You need to say something like:
MyDestinationViewController *viewController = [[MyDestinationViewController alloc] init];
MyCustomizedTransitioningDelegate *transitioningDelegate = [[MyCustomizedTransitioningDelegate alloc]init];
viewController.transitioningDelegate = transitioningDelegate;
viewController.modalPresentationStyle = UIModalPresentationCustom;
[self presentViewController: viewController animated:YES completion:nil];
Or if you're using segues, in prepareForSegue say something like:
MyDestinationViewController *toVC = segue.destinationViewController;
MyCustomizedTransitioningDelegate *transitioningDelegate = [[MyCustomizedTransitioningDelegate alloc]init];
toVC.transitioningDelegate = transitioningDelegate;

UIImagePickerController not full screen

Since the iOS7 upgrade, I have a weird behaviour of the UIImagePickerController. In this application I am using the UIImagePickerController with a cameraOverlayView.
In iOS6 I called the UIImagePickerController using the following code:
_picker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
_picker.sourceType = UIImagePickerControllerSourceTypeCamera;
_picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
_picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
_picker.showsCameraControls = NO;
_picker.navigationBarHidden = NO;
_picker.toolbarHidden = YES;
_picker.wantsFullScreenLayout = YES;
_overlayViewController = [[OverlayViewController alloc] init];
_overlayViewController.picker = _picker;
_overlayViewController.frameSize = self.frameSize;
_overlayViewController.delegate = self;
_picker.cameraOverlayView = _overlayViewController.view;
}
else {
_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
_picker.delegate = self;
Where the OverlayViewController is an UIViewController, with a transparent background which draws some custom controls on screen.
But now in iOS 7 the camera is drawn through the statusbar and a black bar appears beneath the live camera view.
I can solve this by applying a CGAffineTransformMakeTranslation to the cameraViewTransform property of the UIImagePickerController, but why is this like this?
In iOS 7, by default UIViewController views take up the entire screen area including the status bar.
wantsFullScreenLayout
is deprecated and ignored. In some cases, this fix works (in the view controller class):
if ([self respondsToSelector:#selector(setEdgesForExtendedLayout:)]) {
[self setEdgesForExtendedLayout:UIRectEdgeNone];
}
In other cases, it's a bit more complicated. It's late here, so see how you go with it. Helpful things to note - in a UIViewController, the following code will give the correct statusbar height on both iOS 6 and iOS 7, should it come to having to align things using CGRect math:
if (UIDeviceOrientationIsLandscape(self.interfaceOrientation)) {
statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.width;
} else {
statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
}
And then don't forget that in Interface Builder, there are the new "iOS 6 delta" adjustments that allow you to design for iOS 7 and then use offsets to correct for iOS 6.
Anyhow, let me know how you go.
My understanding of the issue, based on a few other SO threads and such, is that UIImagePickerController does not do what we'd expect in terms of managing the status bar via [UIViewController -prefersStatusBarHidden].
This means you either have to disable view controller status bar management entirely, via plist, or figure out a way to get UIImagePickerController to do what we want. On the assumption that you're not looking for the former, I can say I've had success in the latter by putting the picker in a wrapper controller that does what I want (but fall back to your previous code if you still need to detect/support iOS6):
#interface PickerContainer : UIViewController
#property ( nonatomic, weak ) UIImagePickerController* picker;
#end
#implementation PickerContainer
- (void) setPicker: (UIImagePickerController*) picker
{
[self addChildViewController: picker];
[picker didMoveToParentViewController: self];
self->_picker = picker;
}
- (void) viewDidLoad
{
[super viewDidLoad];
self.picker.view.frame = self.view.bounds;
[self.view addSubview: self.picker.view];
}
// Will have no effect in ios6 -- see [-init] for that option
- (BOOL) prefersStatusBarHidden { return YES; }
- (id) init
{
if ( ! ( self = [super init] ) ) return nil;
if ( detectThatThisIsIos6() ) self.wantsFullScreenLayout = YES;
return self;
}
#end
This will work for you, scaled camera, you will have a black bar at the bottom but it will get overlayed by tool bar
https://stackoverflow.com/a/15803947

Why does my MFMailComposeViewController instance only dismiss one time?

I'm trying to track down the source of a bug in a cordova/phonegap plugin I wrote for creating email messages in app using a MFMailComposeViewController instance.
Everyone works fine the first time you present the the composer view. The user can dismiss the mail composer by sending the message or canceling. However, call presentViewController again renders the Cancel and Send buttons in the composer to become useless. My delegate for didFinishWithResult is never calling when pressing the inoperable buttons with the second view of the controller.
Below is simplified repro of what I'm seeing (the simple storyboard has a single view containing a single UIButton wired to my (IBAction)sendMail). What am I doing wrong in obj-c here? Shouldn't I be able to show a controller, dismiss it, and show it again?
ViewController.h:
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#interface ViewController : UIViewController
#end
ViewController.m:
#import "ViewController.h"
#interface ViewController () <MFMailComposeViewControllerDelegate>
#property (nonatomic, weak) IBOutlet UIButton *mailButton;
#property(nonatomic, strong) MFMailComposeViewController* picker;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.picker = [[MFMailComposeViewController alloc] init];
self.picker.mailComposeDelegate = self;
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (IBAction)sendMail
{
[self presentViewController:self.picker animated:YES completion:NULL];
}
#end
The reason for the behavior you are experiencing is the MFMailComposeViewController nils it's delegate when dismissed (maybe in -viewDidDisappear:).
- (void)viewDidLoad
{
[super viewDidLoad];
self.picker = [[MFMailComposeViewController alloc] init];
self.picker.mailComposeDelegate = self;
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
// Put a break point here **#breakpoint1**
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (IBAction)sendMail
{
// Put a break point here **#breakpoint2**
[self presentViewController:self.picker animated:YES completion:NULL];
}
Place breakpoints at shown in the code comment above, run, and follow me as we step through your code.
Tap the interface button that calls your IBAction; execution halts at #breakpoint2
In the console type po self.picker
You'll see the mail compose VC instance is allocated
In the console type po self and then po self.picker.delegate
You'll see these both print the same object (the instance of your view controller)
Resume running, and tap the dismiss button on the mail compose view; execution halts at #breakpoint1
If you want to, inspect local and instance variables in console and then resume running
Tap the interface button that calls your IBAction (this is the second time); execution halts at #breakpoint2
In the console typ po self.picker.delegate
nil is printed to console
This delegate nil'ing behavior isn't documented in either Apple's MFMailComposeViewController class reference or the classes header. It's probably worth filing a bug report requesting clarification and better documentation. Because it's undocumented, the behavior may change in future releases. For that reason, the suggestions to create and destroy the VC as needed certainly seem like good common sense.
This bit me once before. It's caused by the composer being deallocated after it's done being dismissed. To solve this I would place the composer's creation either in viewDidAppear:, or in sendMail as Fahim suggested.
Additionally, you may want to consider wrapping these two lines in [MFMailComposeViewController canSendMail];
I would say take below lines to sendMail... it would work.
self.picker = [[MFMailComposeViewController alloc] init];
self.picker.mailComposeDelegate = self;
You will have as below.
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)sendMail
{
self.picker = [[MFMailComposeViewController alloc] init];
self.picker.mailComposeDelegate = self;
[self presentViewController:self.picker animated:YES completion:NULL];
}
#end
This is working with me...

iOS, ARC: Background thread freezes UI

I have a UIViewController with a UITableView as subview. When a certain cell was clicked an UIImagePickerController should be presented. Because of the long initialization time it takes, I perfom this process in the background when the UIViewController did appear.
Now I added ARC to my project and then it didn't still work. The UI was freezed by the initial process.
Here's my code.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self performSelectorInBackground:#selector(initImagePickerControllerInBackground) withObject:nil];
}
...
- (void)initImagePickerController
{
if (imagePickerController != nil)
return;
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.allowsEditing = YES;
imagePickerController.delegate = self;
imagePickerController.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
}
- (void)initImagePickerControllerInBackground
{
#autoreleasepool
{
[self initImagePickerController];
}
}
What should I change to get it working for me?
UIKit is not a thread safe class and should not be called from outside the main thread. You should not perform this UIImagePickerController allocation/interaction in the background.

How to implement not full screen file preview on ios

I know QLPreviewController could do this. But it's full screen, my requirement is preview files in a subview.
I tried use a offline window to present the QLPreviewController, and then make a screenshot of the offline window.
The problem is i have to show the window, otherwise the screen shot doesn't catch any thing.
Then my question could be, how to make screen shot for offline window in ios ?
Or you may have better ideas of implement file preview in another way.
Any tips will be appreciated.
QLPreviewController can be in a subview.
I self use it in a spliview and a subclassed QLPreviewController.
- (void)tableView:(UITableView *)tView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DocumentViewController *documentViewController = [DocumentViewController new];
[self.navigationController pushViewController:documentViewController animated:YES];
[documentViewController release];
}
where DocumentViewController is a subclass of QLPreviewController:
#interface DocumentViewController : QLPreviewController <QLPreviewControllerDataSource>
#implementation DocumentViewController
...
- (id)init
{
self = [super init];
if (self)
{
self.dataSource = self;
self.delegate = self;
}
return self;
}
...
and implement the methods witch you want (numberOfPreviewItemsInPreviewController is required for the datasource)

Resources