jharr100
jharr100

Reputation: 1469

Memory Issues with UIImageViews

I am trying to use take pictures on an ipad and store the images in an image view - however I keep getting memory warnings and then the program crashes...Is there a way to get around these memory issues with keeping the image...

here is my code:

//the UIImagePickerControllerDelegate

    class CameraDelegate : UIImagePickerControllerDelegate {

        private string meterId = string.Empty;
        private int ext = 0;
        SaveStringElement ele;
        PictureType type;
        NSObject image;
        UIImage picture;
        Section section;
        NSData data;
        Stream s;
        //UIImageView imageView;

        public CameraDelegate(string id, int count, SaveStringElement e)
        {
            type = PictureType.Machine_Meter;
            meterId = id;
            ext = count;
            ele = e;
        }

        public CameraDelegate()
        {
            type = PictureType.Check_MoneyOrder;
        }


        public override void FinishedPickingMedia (UIImagePickerController picker, NSDictionary info)
        {
            switch (type) {
            case PictureType.Machine_Meter:
                try {
                    picker.DismissModalViewControllerAnimated (false);
                    sendToServer(info, meterId +"-"+ext);

                    if (ext == 1) 
                    {
                        MessageBox.Show ("Now picture number two...");
                        TakePicture (meterId, 2, ele, PictureType.Machine_Meter);
                    } 
                    else 
                    {
                        CleanUpScreen (ele);
                    }
                } 
                catch (Exception ex) {
                    var alert = new UIAlertView ("Error processing camera info...", ex.Message + "...try again", null, "OK", null);
                    alert.Show ();
                }
                break;
            case PictureType.Check_MoneyOrder:
                try {
                    picker.DismissModalViewControllerAnimated (false);
                    sendToServer(info, type.ToString());
                } 
                catch {
                }

                break;
            }

            picker.Dispose();
            picker = null;
        }

        void sendToServer(NSDictionary info, string name)
        {
            image = info [NSObject.FromObject ("UIImagePickerControllerOriginalImage")];
            picture = image as UIImage;
            picture.Scale(new SizeF(300, 300));
            imageView = new UIImageView (){Image = picture, ContentMode = UIViewContentMode.ScaleAspectFit, ClipsToBounds= false, Frame = new RectangleF(0,0, 300, 300) };

            if(type == PictureType.Machine_Meter)
            {
                section = new Section () { 
                    HeaderView = imageView
                };

                UpdatePicture (section);
            }

            data = picture.AsJPEG ();
            s = data.AsStream ();

            string Error;
            URMServer.CreateUrmServer ().SaveUploadedPictures (s, currentLocation.ID, type.ToString(), name, out Error);

            image.Dispose();
            image = null;

            picture.Dispose();
            picture = null;

            data.Dispose();
            data = null;

            s.Dispose();
            s = null;

            /*imageView.Image.Dispose();
            imageView.Image = null;
            imageView.Dispose();
            imageView = null;*/

            GC.Collect();
        }
    }

//Take pictures function

void TakePictures (string value, int counter, SaveStringElement e, PictureType pt)
        {
            picker = new UIImagePickerController();
            picker.SourceType = UIImagePickerControllerSourceType.Camera;
            picker.MediaTypes = UIImagePickerController.AvailableMediaTypes(UIImagePickerControllerSourceType.Camera);
            picker.ShowsCameraControls = true;

            switch(pt)
            {
                case PictureType.Machine_Meter:
                    picker.Delegate = new CameraDelegate (value, counter, e);
                    break;
                case PictureType.Check_MoneyOrder:
                    picker.Delegate = new CameraDelegate ();
                    break;
            }



            PresentModalViewController (picker, true);
        }

I add the imageview to a section's headerview to display it...I need this to happen - however if I do this multiple times I get memory warnings and then the app will eventually crash. I have tried to dispose of objects that i am not using and even calling the gc manually...to no avail. The only thing that worked was disposing of the image and image view...however the section header reflects the image view disposal which defeats the purpose...Please let me know if you need any more information...thanks!

Upvotes: 0

Views: 131

Answers (1)

Rolf Bjarne Kvinge
Rolf Bjarne Kvinge

Reputation: 19335

If you need to show the images you've taken to the user (more than one at a time), you should resize them to only use the size you actually need on screen (many full-sized images take a lot of memory, as you've found out, and most of it is not needed when you just want to show the image to the user).

If you need the original full-sized image too, you need to store it to disk first.

Upvotes: 2

Related Questions