I have some code which works on iPhone 5 but not on iPhone 5S or iPad Air. I know that the iPhone 5 is 32 bit and the other devices are 64 bit, but I can't see how this would cause my problem. I use a camera overlay with a button on a tabbar to capture my picture.
I can instantiate the UIImagePickerController like so:
UIImagePickerController *pick = [[UIImagePickerController alloc] init];
pick.modalPresentationStyle = UIModalPresentationCurrentContext;
pick.sourceType = UIImagePickerControllerSourceTypeCamera;
pick.delegate = self;
pick.wantsFullScreenLayout=YES;
/*
The user wants to use the camera interface. Set up our custom overlay view for the camera.
*/
CGSize screenBounds = [UIScreen mainScreen].bounds.size;
CGFloat cameraAspectRatio = 4.0f/3.0f;
CGFloat camViewHeight = screenBounds.width * cameraAspectRatio;
CGFloat scale = screenBounds.height / camViewHeight;
pick.cameraViewTransform = CGAffineTransformMakeTranslation(0, (screenBounds.height - camViewHeight) / 2.0);
pick.cameraViewTransform = CGAffineTransformScale(pick.cameraViewTransform, scale, scale);
pick.showsCameraControls = NO;
/*
Load the overlay view from the OverlayView nib file. Self is the File's Owner for the nib file, so the overlayView outlet is set to the main view in the nib. Pass that view to the image picker controller to use as its overlay view, and set self's reference to the view to nil.
*/
[[NSBundle mainBundle] loadNibNamed:IS_IPAD ? #"OverlayView-ipad" : #"OverlayView" owner:self options:nil];
self.overlayView.frame = pick.cameraOverlayView.frame;
self.focusRect.layer.borderColor = [UIColor colorWithRed:255 green:0 blue:0 alpha:1.0].CGColor;
pick.cameraOverlayView = self.overlayView;
self.overlayView = nil;
self.imagePickerController = pick;
[self presentViewController:self.imagePickerController animated:YES completion:nil];
The focusRect is a Red rectangle on the view to show users how to center their photo.
I have added the protocols to my .h file: UIImagePickerControllerDelegate, UINavigationControllerDelegate
I call takePicture with an IBAction. The following works on 32 bit, but not 64:
- (IBAction)takePicture:(id)sender{
[self.imagePickerController takePicture];
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage]; //64 bit never gets here
//Do some stuff.
}
The symptoms are just that neither didFinishPickingMediaWithInfo nor imagePickerControllerDidCancel ever get executed on 64 bit devices. My overlay is dismissed, but the code in these methods only executes on 32 bit devices.
I have checked several other questions and answers, for example this. Several suggestions from different threads were to make sure that the delegate is being set to self, adding the proper protocols to the view, and making sure the method name is typed correctly. I believe that these are covered.
I have confirmed that the app is running as 32 bit. Due to a dependency, I have the Valid Architectures set to "arm7 arm7s" without arm64. I am using XCode5.I would appreciate any help you can offer. Thank you for looking.
Looks like this was actually not related to the bitness of the device, but how they handled ARC differently.
I solved the problem by moving the dismissViewControllerAnimated from takePicture into the didFinishPickingMediaWithInfo routine like so:
- (IBAction)takePicture:(id)sender{
[self.imagePickerController takePicture];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage]; //64 bit never gets here
//Do some stuff.
[self dismissViewControllerAnimated:YES completion:NULL];
}
My theory is that the object was being destroyed before the delegate method could be called.
Related
I have setup the following to select an image whilst using an IPad. The problem is that the delegate never seems to get called. I've placed breakpoints in but they are never activated.
.H
#interface HomeViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
.M
- (IBAction)loadImage:(id)sender {
self.imagePickerController = [[UIImagePickerController alloc] init];
self.imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.imagePickerController.allowsEditing = NO;
self.imagePickerController.delegate = self;
[self presentViewController:self.imagePickerController animated:YES completion:nil];
}
// This method is called when an image has been chosen from the library or taken from the camera.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//You can retrieve the actual UIImage
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
//Or you can get the image url from AssetsLibrary
NSURL *path = [info valueForKey:UIImagePickerControllerReferenceURL];
[picker dismissViewControllerAnimated:YES completion:nil];
}
Can anyone see the issue?
Thanks
if you used Xcode8 to run the project, please check the project's info.plist, make sure there is a key for "Privacy Photo library usage".
your code is right, maybe the problem is the info.plist.
I had a similar issue and it turned out that the picker was being garbage collected as soon as the image was picked, so the delegate wasn't called.
I needed to ensure that a strong reference to the picker was made before presenting it.
Once that was done, it worked fine.
I set up a UIImagePickerController modal view to record a video, and once the user chooses "Use Video" it dismisses the view controller fine and does exactly what I'd like. However, as soon as I add a cameraOverlayView, which is just a UIView with nothing special going on, didFinishPickingMediaWithInfo is never called. Even if I put an NSLog on the first line of that function, I don't see any output. More oddly still, UIImagePickerControllerDidCancel still gets called when the user presses the Cancel button.
Prior suggestions on SO do not seem helpful as I have set the delegate and set the editing properties accordingly unlike in these popular posts:
uiimagepickercontroller didfinishpickingmediawithinfo NOT CALLED when selecting a video from the library
UIImagePickerController didFinishPickingMediaWithInfo not being called
Here's the code to launch the UIImagePickerController (taken from a Ray Wenderlich tutorial):
-(BOOL)startCameraControllerFromViewController:(UIViewController*)controller
usingDelegate:(id )delegate {
if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO)
|| (delegate == nil)
|| (controller == nil)) {
return NO;
}
if(![delegate conformsToProtocol:#protocol(UIImagePickerControllerDelegate) ]) {
NSAssert(nil, #"delegate muste conforms to UIImagePickerControllerDelegate protocol");
}else{
NSLog(#"delegate does conform to protocol");
}
CGRect screenRect = [[UIScreen mainScreen]bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
CameraOverlay *overlay = [[CameraOverlay alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.delegate = delegate;
cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
cameraUI.allowsEditing = NO;
//if I comment out the line below, everything works fine.
//but if I don't comment this out, didFinishPickingMediaWithInfo is never called
cameraUI.cameraOverlayView = overlay;
[controller presentModalViewController: cameraUI animated: YES];
return YES;
}
Here's the part of didFinishPickingMediaWithInfo:
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"did finish picking media with info");
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:NO];
// I also tried [picker dismissModalViewControllerAnimated:NO];
//despite it not being its own delegate, but in any case
//this did not change the result
...
}
I added the valid delegate check following Andrey's advice, but now I always see "delegate does conform to protocol" regardless of whether the overlay is in and didFinishPickingMediaWithInfo is called.
Any suggestions for what to try or errors in the code? Thanks for taking a look.
Here's what finally worked: set userInteractionEnabled to FALSE for the overlay view.
I wish I knew whether this was a bug or a feature. I looked at a few tutorials for creating overlay views, and none mentioned setting this property. If anyone can fully explain why this made the difference, I'd like to mark a more complete answer than this.
#sunny finally correct solution! userInteractionEnabled = FALSE did call didFinishPickingMediaWithInfo
Hi Im making altered reality app. My main controller is derived from UIImagePickerController. Here is how I create it:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (!self.overlayController) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.sourceType = UIImagePickerControllerSourceTypeCamera;
self.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
self.cameraDevice = UIImagePickerControllerCameraDeviceRear;
self.showsCameraControls = false;
self.allowsEditing = false;
self.overlayController = [[[ControlsViewController alloc] initWithNibName:#"ControlsViewController" bundle:[NSBundle mainBundle] picker:self] autorelease];
[self.view addSubview:self.overlayController.view]; // need to add as subview otherwise mouse events captured by UIImagePickerController
self.overlayController.view.frame = CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height);
}
}
[cameraControls setCamera:self];
}
Basically controller exists through whole lifecycle of the app. Now when I take photo with code below most of the time it works fine but sometime didFinishPickingMediaWithInfo isn't called. Usual behavior is I see camera focus starts adjust blur-in/out and then it stabilizes but didFinishPickingMediaWithInfo will never be called. Looks like if camera goes into some kind of calibration mode it may not fire this event. Anyone have solution? I hope there is maybe some kind of extra callback (like error processing Im missing).
I can even hear camera shutters simulated sound interrupted when camera goes into that weird calibration mode.
//self.cameraPicker points to the instance of my main controller that I created earlier
-(void) setCamera:(UIImagePickerController *)picker {
self.cameraPicker = picker;
picker.delegate = self;
}
-(void) takePicture {
[self.cameraPicker takePicture];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"Picture ready");
}
I have a UICollectionView and a bar button at top right(CameraViewController1 : UICollectionViewController).The flow is when I take a picture it moves to a new view controller where the image can be cropped.User has two option Use and Cancel after choosing any of this option it gets back the image to the collection view and it gets arranged like cells.I want to take many photos.But I can take up to 3 pictures only where as the app crashes immediately and shows a message "App terminated due to memory pressure".But the worst part is when I tested the same app in iPhone 5 running iOS 7 the crash wasn't happen.When I test the same in iPhone 4 running iOS 7 it gets crashed and produce received memory warning.
Here my code
- (IBAction)TakeaPhoto:(id)sender {
[[UIApplication sharedApplication]setStatusBarHidden:FALSE withAnimation:NO];
gallery=0;
picker1 = [[UIImagePickerController alloc] init];
picker1.delegate = self;
self.resizeableCropArea =YES;
self.cropSize=CGSizeMake(300,350);
//picker1.allowsEditing = YES;
picker1.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker1 animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:NULL];
UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage];
image_cap = [self imageTemp:image scaledToSize:CGSizeMake(320, 370)];
dataTemp = UIImageJPEGRepresentation(image,0.0);
CropViewController *cropController = [[CropViewController alloc] init];
cropController.sourceImage = [info objectForKey:UIImagePickerControllerOriginalImage];
Original_img = UIImageJPEGRepresentation(cropController.sourceImage,0.0);
[original_image addObject:[UIImage imageWithData:Original_img]]; //original_image Nsmutablearray
NSLog(#"source image=%#",cropController.sourceImage);
cropController.resizeableCropArea = self.resizeableCropArea;
cropController.cropSize = self.cropSize;
cropController.delegate = self;
Cancel_Image= cropController.sourceImage;
[self.navigationController pushViewController:cropController animated:YES];
}
#Ramanan R R, I m totally agree with the #Rushabh's comment..
You are allocating that UIImagePickerController for many more times, as TakeaPhoto method call you are allocating UIImagePickerConrtoller, it is not necessary to allocate that multiple times. It makes memory spoilage, thats why your app is going to terminate or crash..
Just allocate that one time in viewDidLoad, make sure one more thing that, do
UIImagePickerController as a strong property, because in past it took my whole day to solve issue...
Hope this will work for you and your app will run smoothly...:)
Im getting a memory warning when Im using the camera on an iPhone. Im also using ARC.
When you take a photo and press the 'use Photo' button on the camera view controller I get a memory warning. The intention is once the 'use Photo' button is pressed that it changes the contents of the an ImageView.
I thought the memory issue might be due to the fact that the image that is captured is full screen, and the ImageView is 250h 250w. But I tried scaling down the size of the image taken by the camera and then assign it to the ImageView. However this still did not work, even when I resized it to 100 x 100.
Secondly, I then did not assign the photo taken by the camera to the ImageView but it still has the memory warning.
I looked at other answers here and attempted the two above but it is still there. I will show my code below. Will this affect my submission to the app store? Surely if it is such a common occurence that it is a bug or there is a work around? It would be great if one could look at the code provided and spot the error or suggest how to handle this memory warning?
My app is 95+% finished apart from this memory warning so it is getting close to submission time.
My code:
- (IBAction)takePhoto:(id)sender {
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing=NO;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:self.imagePicker animated:YES completion:NULL];
}
else{
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:self.imagePicker animated:YES completion:NULL];
}
}
- (IBAction)choosePhoto:(id)sender {
self.imagePicker2 = [[UIImagePickerController alloc] init];
self.imagePicker2.delegate = self;
self.imagePicker2.allowsEditing=NO;
[self.imagePicker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:self.imagePicker2 animated:YES completion:NULL];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
CGRect rect = CGRectMake(0,0,100,100);
UIGraphicsBeginImageContext( rect.size );
[self.image drawInRect:rect];
UIImage *picture1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.snapImage setImage:picture1];
[self.uploadImageBtn setHidden:NO];
[self dismissViewControllerAnimated:YES completion:NULL];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:NULL];
}
I didnt find a good solution but I would not store the raw image in a property because the raw image takes up roughly 30MB of memory. So instead of:
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
I changed it to:
UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
This way the image is destroyed when it is no longer in use. Note: I've test this new method on iPhone 4 series and 5. The memory warning only appears on the 4 series not the 5.
From looking around the web there have been many bug reports submitted to Apple in regards to the Camera and iOS7. For instance, irregularly when you launch the Camera it will give a black preview - this is linked to iOS7, and more so the iPhone 4 series not 5. This is probably the difference in the processor power - but I am not sure. My app got approved for the app store so the memory warning will not be an issue –
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
Clearing the Cache in the class i was using the "UIImagePickerController", worked for me !!!