locked
issue with camera function in my iOS 8 app RRS feed

  • Question

  • User141987 posted

    Hello I am working on an iOS app that has camera functionality built into it. I want the user to be able to take a picture and save it to their camera roll here’s my code so far:

    using Foundation;
    using System;
    using System.CodeDom.Compiler;
    using UIKit;
    
    namespace ToolBelt.iOS
    {
                    public partial class cameraViewController : UIViewController
                    {
                                    public cameraViewController (IntPtr handle) : base (handle)
                                    {       
                                    }
    
    
    
                                    public override void DidReceiveMemoryWarning ()
                                    {
                                                    // Releases the view if it doesn't have a superview.
                                                    base.DidReceiveMemoryWarning ();
    
                                                    // Release any cached data, images, etc that aren't in use.
                                    }
    
                                    public override void ViewDidLoad ()
                                    {
                                                    base.ViewDidLoad ();
    
    
                                                    //UIPopoverController popover = new UIPopoverController (ctrl);
    
                                                    // Perform any additional setup after loading the view, typically from a nib.
                                    }
    
                                    public override void ViewDidAppear (bool animated)
                                    {
                                                    base.ViewDidAppear (animated);
    
                                                    UIImagePickerController picker = new UIImagePickerController ();picker.PrefersStatusBarHidden ();
    
                                                    picker.SourceType = UIImagePickerControllerSourceType.Camera;
                                                    PresentViewController(picker, true, () => {});
                                    }
    
    
                    }
    }
    

    When I use this code it will let me take the photo but when I click the use photo button nothing happens and I get this error in the debugger:

    Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

    Any help would be amazing I have to have this fixed by September 14th

    Regards

    Chris

    Wednesday, September 9, 2015 3:47 PM

All replies

  • User181 posted

    First, I hate to tell you this, but if you're hoping to have a fix in the app store by the 14th you're SOL. The app review times are likely to go up to about 2 weeks as a huge number of app developers try to push out updates for iOS 9. You've missed your target already if you waited this long.

    Second, if you could attach an example project that reproduces the issue then it would be easier to help you figure out the problem.

    Wednesday, September 9, 2015 10:32 PM
  • User141987 posted

    hello I have attached an example of the camera function from my project to a test project it builds just fine. and I have until the 14th to get this fixed I have until the 10 of October to get it on the app store :) thank you for all your help!

    Thursday, September 10, 2015 1:16 PM
  • User181 posted

    Sorry it took so long, but since I had to use an actual device it took me a while to have time to look into this. First, the message printed to the console about snapshotting is a red herring. Ignore that. It doesn't matter.

    I don't know what your real code looks like, but your example code doesn't do anything with the results so obviously choosing "Use Photo" isn't going to do anything. It just dismisses the camera, which causes ViewDidAppear to get called, which re-shows the camera view. You have to implement one of the event callbacks (FinishedPickingImage or FinishedPickingMedia) to do anything useful with the results, and if you implement one of those you then have to dismiss the picker yourself. This is what I tried:

        public override void ViewDidAppear (bool animated)
        {
            base.ViewDidAppear (animated);
    
            if (imageView.Image == null)
            {
                UIImagePickerController picker = new UIImagePickerController ();
    
                picker.SourceType = UIImagePickerControllerSourceType.Camera;
                picker.FinishedPickingImage += (sender, e) =>
                {
                    DismissViewController(true, null);
                    imageView.Image = image;
                };
                PresentViewController (picker, true, null);
            }
        }
    

    (I added a UIImageView to the view controller to show the results.)

    The interesting thing is that this code didn't work. Maybe this is what you actually tried in your real code (if so then, please, in the future make sure your example code actually does the same thing as your real code so that people can see what is actually going wrong without making too many assumptions). When I pressed the "Use Photo" button this time absolutely nothing happened. Strange. I did a quick Google search and came up with this (embarrassingly old) bug report. That seems like the issue. In fact, if you switch to FinishedPickingMedia then the button works, and you can dismiss the view controller, but of course you can't get the image because that event doesn't give you an image (it gives you a URL). So the real fix was to use the Delegate property instead of the event:

        public override void ViewDidAppear (bool animated)
        {
            base.ViewDidAppear (animated);
    
            if (imageView.Image == null)
            {
                UIImagePickerController picker = new UIImagePickerController ();
    
                picker.SourceType = UIImagePickerControllerSourceType.Camera;
                picker.Delegate = this;
                PresentViewController (picker, true, null);
            }
        }
    
        [Export ("imagePickerController:didFinishPickingImage:editingInfo:")]
        public void FinishedPickingImage (UIKit.UIImagePickerController picker, UIKit.UIImage image, Foundation.NSDictionary editingInfo)
        {
            DismissViewController(true, null);
            imageView.Image = image;
        }
    

    Now it works as expected.

    For more information about delegates, events, and how they all fit together see this video.

    Saturday, September 12, 2015 9:24 PM
  • User141987 posted

    @AdamKemp Thank you so much for the reply. I have implemented the example into my project, however I left my lighting cable at home so I will test it tonight and let you know how it goes! once again thank you for all your help :)

    Monday, September 14, 2015 1:48 PM
  • User141987 posted

    @AdamKemp I just got around to testing the app it does take the picture but once I click the use photo button I get the image but no way to save it and I'm stuck on this page how would you save it to the camera roll and have it go back to my app?

    I've attached a screenshot of what i'm getting

    Once again thank you for all your help! :)

    Tuesday, September 15, 2015 2:46 PM
  • User181 posted

    There is a method on UIImage called SaveToPhotosAlbum. As for going back to your app that's done already in the example code I showed you. That's what DismissViewController does.

    Tuesday, September 15, 2015 4:29 PM